标签: function-composition

Tupled函数组合

我很好奇为什么会这样

let f = (fun a b -> a, b) >> obj.Equals
Run Code Online (Sandbox Code Playgroud)

给出了错误

没有名为'Equals'的可访问成员或对象构造函数需要1个参数

但这很有效

let f = (fun a -> a, a) >> obj.Equals
Run Code Online (Sandbox Code Playgroud)

f# function-composition

3
推荐指数
1
解决办法
137
查看次数

带有函数组合的类型推断列表

我试图使用折叠来取得Haskell中整数之和的平方.但是,我从GHCi那里得到了一个神秘的错误.这是我的单行:

((^2) . foldl) (+) 0 [1..100]
Run Code Online (Sandbox Code Playgroud)

我从GHCi得到的是:

Prelude> ((^2) . foldl) (+) 0 [1..100]

<interactive>:19:3:
    No instance for (Num (b0 -> [b0] -> b0))
      arising from a use of `^'
    Possible fix:
      add an instance declaration for (Num (b0 -> [b0] -> b0))
    In the first argument of `(.)', namely `(^ 2)'
    In the expression: (^ 2) . foldl
    In the expression: ((^ 2) . foldl) (+) 0 [1 .. 100]
Run Code Online (Sandbox Code Playgroud)

我认为问题出在我最后根据此类型声明传入的列表中.

Prelude> :t ((^2) . foldl) (+) 0 …
Run Code Online (Sandbox Code Playgroud)

haskell ghci function-composition

3
推荐指数
2
解决办法
175
查看次数

Haskell函数组成,(.)(.)的类型以及它是如何呈现的

所以我知道:

(.) = (f.g) x = f (g x)
Run Code Online (Sandbox Code Playgroud)

它的类型是(B-> C) - >(A-> B) - > A-> C但是怎么样:

(.)(.) = _? = _?
Run Code Online (Sandbox Code Playgroud)

这是如何表示的?我想到了:

(.)(.) = (f.g)(f.g)x = f(g(f(g x))) // this
(.)(.) = (f.g.h)x = f(g(h x)) // or this
Run Code Online (Sandbox Code Playgroud)

但就我试图获得它的类型而言,GHCi告诉我的不正确.那么什么都是"_?"

另外 - 函数/运算符$做什么?

haskell types function-composition operator-keyword

3
推荐指数
2
解决办法
1988
查看次数

我对组成的理解差距,类型不匹配

在我的代码中,我需要(Int, Int)按第二个元素的值对s 列表进行排序.

这很容易做到sorted = sortBy (\(_,a) (_,b) -> compare a b) listOfPairs,但我讨厌像这样写lambda,我需要在我的代码中的几个地方使用相同的lambda.

所以我试着创建两个有用的函数,我最终会在很多地方使用如下(我在前奏中找不到它们):

-- apply a function of one argument to two different arguments and return a pair
fBoth :: (a -> b) -> a -> a -> (b,b)
fBoth f a b = (f a, f b)

-- pass elements of a pair to a function of two arguments
expandPair :: (a -> b -> c) -> (a,b) -> c
expandPair f (a,b) = …
Run Code Online (Sandbox Code Playgroud)

lambda haskell function function-composition

3
推荐指数
1
解决办法
129
查看次数

项目Euler 3 - Haskell

我正在努力解决Haskell中的Project Euler问题.我已经得到了下面的问题3的解决方案,我已经在小数字上进行了测试并且它可以正常工作,但是由于通过首先导出所有素数数字来实现蛮力,所以对于较大的数字它是指数级慢的.

-- Project Euler 3

module Main 
    where

import System.IO
import Data.List

main = do
    hSetBuffering stdin LineBuffering
    putStrLn "This program returns the prime factors of a given integer"
    putStrLn "Please enter a number"
    nums <- getPrimes
    putStrLn "The prime factors are: "
    print (sort nums)


getPrimes = do
    userNum <- getLine
    let n = read userNum :: Int
    let xs = [2..n]
    return $ getFactors n (primeGen xs)

--primeGen :: (Integral a) => [a] -> [a]
primeGen [] …
Run Code Online (Sandbox Code Playgroud)

primes haskell pointfree function-composition

3
推荐指数
1
解决办法
442
查看次数

用相同的arg类型组合2(或n)('a - >单位)函数

是否有某种形式的内置/术语我不知道它有点但不同的"组成"两个'a -> unit功能产生一个单一的功能; 例如:

let project event =
    event |> logDirections
    event |> stashDirections
let dispatch (batch:EncodedEventBatch) =
    batch.chooseOfUnion () |> Seq.iter project
Run Code Online (Sandbox Code Playgroud)

可能成为:

let project = logDirections FOLLOWEDBY stashDirections
let dispatch (batch:EncodedEventBatch) =
    batch.chooseOfUnion () |> Seq.iter project
Run Code Online (Sandbox Code Playgroud)

然后:

let dispatch (batch:EncodedEventBatch) =
    batch.chooseOfUnion () |> Seq.iter (logDirections FOLLOWEDBY stashDirections)
Run Code Online (Sandbox Code Playgroud)

我想有人可能会比较它tee(如FSFFAP的铁路导向编程系列中提到的那样).

(它需要将相同的arg传递给两者并且我正在寻求按顺序运行它们而没有任何异常处理技巧问题等)

(我知道我可以做,let project fs arg = fs |> Seq.iter (fun f -> f arg)但我想知道是否有内置的和/或某种形式的组合库我不知道)

f# action side-effects composition function-composition

3
推荐指数
1
解决办法
149
查看次数

如何撰写bool返回函数

我有几个带签名的过滤功能'a -> bool.我想创建一个组合过滤器,AND是不同的过滤器.我知道我可以这样做:

let fCombined x =
    f1 x 
    && f2 x
    && f3 x
Run Code Online (Sandbox Code Playgroud)

有没有更简洁的方法来直接组合函数而不完全应用它们(例如,x从定义中删除fCombined)?

(我知道还有其他不使用的方法bool,例如使用'a -> 'a option函数并使用它们编写它们Option.bind,但这几乎不简洁.)

f# function-composition

3
推荐指数
1
解决办法
79
查看次数

简化Elm中的两参数匿名函数

在榆树中,如果我有匿名功能

(\f x -> f x)
Run Code Online (Sandbox Code Playgroud)

我可以简化为

(<|)
Run Code Online (Sandbox Code Playgroud)

对于参数为另一个函数的参数的两参数函数,可以这样做吗?

(\x y -> f x y |> g)
Run Code Online (Sandbox Code Playgroud)

我以为我可以用

(f |> g)
Run Code Online (Sandbox Code Playgroud)

但是编译器抱怨类型。

具体来说,在我的update函数的一种情况下,我有类似以下内容:

let
  msgNames = [Foo, Bar]

  values = ["f", "b"] // These values are actually derived
                      // by a more complicated operation

  model_ = List.map2 (<|) msgNames values
               |> List.foldl (\msg mod -> update msg mod |> Tuple.first)
                      model
in
  ( model_, Cmd.none )
Run Code Online (Sandbox Code Playgroud)

我试图简化匿名函数参数List.foldl喜欢的东西(update |> Tuple.first),但我从编译器以下错误:

The right side of (|>) …
Run Code Online (Sandbox Code Playgroud)

function-composition elm

3
推荐指数
1
解决办法
611
查看次数

在Haskell中部分应用几个函数

假设,在Haskell中,我有一堆函数都依赖于相同的参数类型:

f :: Par -> a -> b
g :: Par -> b -> c
Run Code Online (Sandbox Code Playgroud)

由于我正在编写更多依赖于此参数类型的函数,我可以执行类似的操作

h :: Par -> a -> c
h par = myg . myf
    where myf = f par
          myg = g par
Run Code Online (Sandbox Code Playgroud)

但是我不得不写这些where线.问题是:这可以避免吗?

[编辑:我试图提供一个最小的例子来说明问题,但显然这个例子太小而无法说明我想要的东西.在实际问题中,h当然不仅仅是f和g的组成.所以这是一些实际的代码:

有功能

apply :: ChamberLattice -> ChLatword -> ChLatWord
reduce :: ChamberLattice -> ChLatWord -> ChLatWord
Run Code Online (Sandbox Code Playgroud)

我正在定义一个函数

chaseTurn :: ChamberLattice -> Turn -> Parity -> ChLatWord -> ChLatWord
chaseTurn cl Straight _ xs = xs
chaseTurn cl t parity …
Run Code Online (Sandbox Code Playgroud)

haskell function function-composition

3
推荐指数
2
解决办法
156
查看次数

Composing PartialFunctions with orElse when there is a wildcard case

Is it possible to orElse compose two PartialFunctions when the first function has case _ => wildcard pattern that matches anything thus in effect being a total function.

For example, given

val pf1: PartialFunction[Int, String] = {
  case 1 => "foo"
  case 2 => "bar"
  case _ => "wildcard"
}

val pf2: PartialFunction[Int, String] = {
  case 3 => "baz"
  case _ => "wildcard"
}
Run Code Online (Sandbox Code Playgroud)

then, out-of-the-box

(pf1 orElse pf2)(3)
Run Code Online (Sandbox Code Playgroud)

outputs wildcard. However, assuming we cannot modify pf1 …

scala function-composition

3
推荐指数
1
解决办法
56
查看次数