How can you correctly associate F # functions in a string for a set of operations performed in a list?

So if I have a simple list in F #

let ls = [3; 9; 2; 15; 3]

And I want sortDescending, and then accept 3. I can do this chaining:

let orderList ls = List.sortDescending ls
let take3 ls = List.take(3) ls
let main ls = (orderList >> take3) ls

And this works great and will work for what I want when using the F # Interactive debugger. But I'm trying to do this inline in all one expression and not get the syntax correctly. Is this possible, and I just don't get the syntax.

let main2 ls = List.sortDescending >> List.take(3) ls

I am moving from more object-oriented thinking, where in C # I can do this:

var ls = new List<int>{ 3, 9, 2, 15, 3 };
var subset = ls.OrderByDescending(x => x).Take(3).ToList();

And I just expected you to be able to continue the chain like the smooth INLINE syntax, but it seems like I'm missing the syntax or not getting something simple. I recently studied F #, but so far I'm digging it. It’s just not easy for me to catch my head around the fact that things are related to each other.

+4
3

>> , .

|> , .

,

let addone x = x + 1
let double x = x * 2

(addone >> double) 100 100 |> addone |> double 202. , ? , .

. f >> g - :

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

So

(addone >> double) 100

(fun x -> double ( addone x ) ) 100

,

double (addone 100)

x |> f - pipe:

let pipe x f = f x

So

100 |> addone |> double

(addone 100) |> double

double (addone 100)

# - :

customers.Take(100).ToList()

Enumerable.ToList(Enumerable.Take(customers, 100))
+15

, , F #. :

let main2 ls = List.sortDescending >> List.take(3) ls

, , , , List.take , . , F # . . . . .

List.take (3) (ls)
List.take 3 ls

, . . , .

(3 * 6) + 10
3 * 6 + 10

, . , . 6 + 10, 3.

3 * (6 + 10)

F # . , . 3. . , .

(3) * (6) + (10)

:

sqrt(2.0) + 2.0
sqrt 2.0 + 2.0

. , , . , , :

(sqrt 2.0) + 2.0

(2.0) . , 4.0, :

sqrt (2.0 + 2.0)

. 3 , , - :

let main2 ls = List.sortDescending >> List.take(3) ls
let main2 ls = List.sortDescending >> (List.take 3 ls)

. List.take 3 ls. List. List.sortDescending List. . , , List.take List.sortDescending. :

(List.take 3)

:

let main2 ls = (List.sortDescending >> (List.take 3)) ls
let main2 ls = (List.sortDescending >> List.take 3) ls

. , , - List.take 3, List.sortDescending. , ls . , . "" :

let main2 ls = (List.sortDescending >> List.take 3) ls
let main2 = List.sortDescending >> List.take 3

, " ", . , , . , :

let main ls = ls |> List.sortDescending |> List.take 3

, :

let main ls = List.take 3 (List.sortDescending ls)

Btw. , . List.truncate List.take. List.take , . List.truncate .

List.take     4 [1;2;3] // throws an exception
List.truncate 4 [1;2;3] // returns -> [1;2;3]

, . , : http://sidburn.imtqy.com/blog/2016/09/25/function-application-and-composition

+2

linq F #.

open System
open System.Linq


let ls = [3; 9; 2; 15; 3]
ls.OrderByDescending(fun x -> x).Take(3).ToList()

, , Fsharp.Core.Fluent :

#r @"..\packages\FSharp.Core.Fluent-4.0.1.0.0.5\lib\portable-net45+netcore45+wpa81+wp8+MonoAndroid1+MonoTouch1\FSharp.Core.Fluent-4.0.dll"
#r "System.Runtime"
open FSharp.Core.Fluent
open System.Runtime

ls.sortDescending().Take(3).toList()

Note the difference between .ToList()which is a general list of .NET: Collections.Generic.List<int> = seq [15; 9; 3]and .ToList()that is associated with # the F: val it : int list = [15; 9; 3].

+1
source

Source: https://habr.com/ru/post/1657654/


All Articles