
[英]Is there a way to add children components to functional stateless components that are called as functions?
[英]What are those functional functions called?
我正在寻找一种实现此功能的方法:
list = [a b c d e f]
foo(list, 3) = [[a d] [b e] [c f]]
潜在的解决方案是:
foo(list,spacing) = zip(goo(list,spacing))
例如,
goo([a b c d e f],3) = [[a b c] [d e f]]
foo
和goo
通常称为什么,因此我可以寻找现有的解决方案,而不必重新发明轮子了?
注意:我只是想展示一些示例,希望可以更轻松地进行操作,而不是尝试用语言解释。 任意语法可提供更广泛的理解。
您可以使用partition :
(partition 3 '[a b c d e f])
=> ((a b c) (d e f))
(partition 2 '[a b c d e f])
=> ((a b) (c d) (e f))
编辑:
(apply map list (partition 3 '[a b c d e f]))
=> ((a d) (b e) (c f))
我认为没有内置函数。 这很容易实现。
我知道您不希望实现,但是其中一个标记是Haskell,所以也许您想看看
p :: Int -> [a] -> [[a]]
p n xs = [ [x | (x ,y) <- ys , y `mod` n == i] | i <- [0 .. n - 1] , let ys = zip xs [0 .. ]]
那很实用。
您的goo
函数是带有翻转参数的drop
。 鉴于此,您可以像在问题中所说的那样实现foo
:
let foo list spacing = zip list (drop spacing list)
尽管这仍然不能完全提供您需要的结果,但是请关闭:
Prelude> foo "abcdef" 3
[('a','d'),('b','e'),('c','f')]
编辑:
仔细阅读后,您的goo
函数将带翻转参数的splitAt
。 鉴于此,可以这样定义foo
:
let foo list spacing = (uncurry zip) $ splitAt spacing list
与以下内容相同:
let foo list spacing = let (left, right) = splitAt spacing list
in zip left right
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.