From 7a13ac2369db00719fba8541d04f1c4b0d520eb6 Mon Sep 17 00:00:00 2001 From: Takafumi Arakaki Date: Thu, 14 Jan 2021 17:30:06 -0800 Subject: [PATCH] Fix keyword argument handling (#18) --- src/FGenerators.jl | 3 +++ test/test_samples.jl | 28 +++++++++++++++++++++++++++- 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/src/FGenerators.jl b/src/FGenerators.jl index b55bce3..fd1db90 100644 --- a/src/FGenerators.jl +++ b/src/FGenerators.jl @@ -180,6 +180,9 @@ macro fgenerator(ex) end allargs = map([def[:args]; def[:kwargs]]) do x + if isexpr(x, :kw) + x = x.args[1] + end if @capture(x, args_...) args elseif @capture(x, a_::T_) diff --git a/test/test_samples.jl b/test/test_samples.jl index 90b454e..cbbaaef 100644 --- a/test/test_samples.jl +++ b/test/test_samples.jl @@ -94,7 +94,29 @@ end end end -raw_testdata = """ +asval(x::Val) = x +asval(x) = Val(x) +const FlagType = Union{Val{true},Val{false},Bool} + +@fgenerator function linesin(str::AbstractString; keep::FlagType = Val(false)) + keep = asval(keep) + start = firstindex(str) + for (i, c) in pairs(str) + if c == '\n' + if keep === Val(true) + @yield SubString(str, start, i) + else + @yield SubString(str, start, prevind(str, i)) + end + start = nextind(str, i) + end + end + if start <= ncodeunits(str) + @yield SubString(str, start) + end +end + +raw_testdata = raw""" noone() == [] oneone() == [1] onetwothree() == [1, 2, 3] @@ -113,6 +135,10 @@ ffilter(isodd, oneone()) == [1] ffilter(isodd, onetwothree()) == [1, 3] ffilter(isodd, 1:5) == [1, 3, 5] ffilter(isodd, organpipe(3)) == [1, 3, 1] +linesin("a\nbb\nccc") == ["a", "bb", "ccc"] +linesin("a\nbb\nccc"; keep=true) == ["a\n", "bb\n", "ccc"] +linesin("a\nbb\nccc\n") == ["a", "bb", "ccc"] +linesin("a\nbb\nccc\n"; keep=true) == ["a\n", "bb\n", "ccc\n"] """ args_and_kwargs(args...; kwargs...) = args, (; kwargs...)