是否存在与Perl或Ruby中的触发器运算符等效的函数式编程概念?

And*_*imm 7 ruby perl functional-programming flip-flop

Ruby(和Per​​l)有一个触发器的概念:

file = File.open("ordinal")
while file.gets
  print if ($_ =~ /third/) .. ($_ =~ /fifth/)
end
Run Code Online (Sandbox Code Playgroud)

给出了一系列序数,例如

first
second
third
fourth
fifth
sixth
Run Code Online (Sandbox Code Playgroud)

当它达到"第三"时开始打印,当达到"第五"时停止打印:

third
fourth
fifth
Run Code Online (Sandbox Code Playgroud)

是否存在类似于此的函数式编程概念,或者通常用takewhiles 来描述?我不是在问一个特定的语言,而是用你用来形容它的术语.

Dav*_*ani 8

在诸如haskell之类的函数式语言中,您可以将翻转和翻转条件作为谓词传递,并根据它过滤输入列表.例如,以下是flipflophaskell 的定义(如果您不知道haskell,请不要担心实现 - 关键部分是如何使用它):

flipflop flip flop = 
  uncurry (++) . second (take 1) . break flop . dropWhile (not . flip)
Run Code Online (Sandbox Code Playgroud)

这是它的使用方法:

> flipflop (== 3) (== 5) [1..10]
[3,4,5]
Run Code Online (Sandbox Code Playgroud)

这是通过使用更高阶函数来制作有效的新语言构造的一个例子.

我不知道在函数式语言中是否有该特殊名称.