質問

マイクロソフトマイクロソフトの F#サンプル, ものを用いている">>"オペレーターとして

test |> Seq.iter (any_to_string >> printfn "line %s");

どのような">>"オペレーターだ。各項目の配列(配列の場合)に渡される any_to_string 暗黙のうちに?このと同様 (fun item -> printfn "line %A" item)?

役に立ちましたか?

解決

同等のコードを書くことができ、以下の方法:


test |> Seq.iter(fun x -> printfn "line %s" (any_to_string x))

つまり、>>オペレーターだけでは:される関数f(x)の還元タイプT、g(y)とyのシリコーンコーティング処理を行使f>>gの作成機能h(z)が相当g(f(x)).引数を指定しないが、内側と外側の機能に渡されるオペレーターの機能に応用できる時間コードで、なにができること:


//myFunc accepts any object, calls its ToString method, passes ToString
//result to the lambda which returns the string length. no argument is
//specified in advance
let myFunc = any_to_string >> (fun s -> s.Length)
let test = 12345
let f = 12345.0
//now we can call myFunc just like if we had definied it this way:
//let myFunc (x:obj) = obj.ToString().Length
printfn "%i" (myFunc i)
printfn "%i" (myFunc f)

他のヒント

(>>) 高次機能する機能(対応の引数)と組み合わせ("を構成す")していく一つの機能です。

のためのインスタンス

let len (s : string) = s.Length
let show (n : int) = n.ToString()

の線

(show >> len) 15

に相当

len (show 15)

など

show 15 |> len

の定義で

http://research.microsoft.com/en-us/um/cambridge/projects/fsharp/manual/FSharp.Core/Microsoft.FSharp.Core.Operators.html

という

val ( >> ) : ('a -> 'b) -> ('b -> 'c) -> ('a -> 'c)
//Compose two functions, the function on the left being applied first

していますが、なんとかその他の提供を深く掘り下げて説明を受けます。

編集

MSDN doc今

http://msdn.microsoft.com/en-us/library/ee353825(VS.100).aspx

この機能構成オペレーター(その他)

をもつように設定できます。オペレーターにより自分を見るために彼のセマンティクス

let (>>) f g = fun x -> g (f x)

この助しない場合があるスレッドセーフで、C#、ジェネリック医薬品、lambdasのだが、ここでは、同等クライアントまで、フルのC#:

//Takes two functions, returns composed one
public static Func<T1, T2> Compose<T1, T2, T3>(this Func<T1, T2> f, Func<T2, T3> g)
{
    return (x) => g(f(x));
}

みの型パラメータを読み込みのようなものBrianの回答:

構成は、一つの機能からT1T2、他からはT2~T3を返しますので、からT1~T3.

>> オペレーターを行う機能構成に説明するかも をWikipediaで調べる.ダスティキャンベルの機能を利用で説明では |> (パイプ)オペレーター) 彼のブログ.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top