Skip to content

Instantly share code, notes, and snippets.

@mat3u
Created January 16, 2015 12:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mat3u/612352a76fb4829682c4 to your computer and use it in GitHub Desktop.
Save mat3u/612352a76fb4829682c4 to your computer and use it in GitHub Desktop.
Functional Friday
// Functional Friday - Introduction
// Mateusz Stasch <mstasch@future-processing.com>
// 2014.12.12
// Links:
// http://fsharp.org
// http://tryfsharp.org
// http://fsharpforfunandprofit.com
// F# is a mature, open source, cross-platform, functional-first programming language
module Values =
// Type inference
let x = 5
let f = 5.0
let s = "Hello F#"
let sv = @"Verbatim string"
let svv = """Very verbatim "string"!!!"""
let ``afsdf fsd ad_ dsfs let a = 5`` = 5
let g = 0
let g' = (* derivative *) g
let g'' = (* derivative *) g'
// let x = 6
// x = 6
// Mutable variables - don't do that
// Everything is a value!
let a = 2
let b = 4
let r = if a > b then a else b
let r2 = if b < a then ignore b; ignore a;b; else a;
module Collections =
let list = [1,2,3,4]
let array = [|5,6,7,8|]
// Don't do that
let elem = array.[2]
// Lazy evaluation
let sequence = seq {
yield 1
yield 2
printfn "Lazy evaluation"
yield 3
yield! [1;2;3]
}
let firstTwo = Seq.take 2 sequence
let all = Seq.toList sequence
// .. operator
let g = [0 .. 100]
let g10 = [0 .. 10 .. 100]
// :: - add head (typical list)
let c1 = 5 :: g
//let c2 = g :: 5
// @ - collection concat
let c3 = c1 @ g
// x::xs - pattern matching with lists
let head::tail = c3
module Functions =
let f x = x * 2
printfn "%i" (f 5)
// Higher order functions
let executeAndSayThat someFunction v =
printfn "Wykonuję..."
someFunction v
executeAndSayThat f 24
let isEven x = x % 2 = 0
let data = { 0 .. 20 }
let result = Seq.filter isEven data
printfn "%A" result
let rec factorial n =
if n < 1 then 1
else n * (factorial n - 1)
// Mutually recursive (...and)
module Currying =
let sum a b = a + b
let addTwo = sum 2
addTwo 5
// Describe: sum: int -> int -> int
module ``Higher-order functions`` =
let input = [1;2;3;4;5;6]
let filtered = List.filter (fun x -> x % 2 = 0) input
module ``Pipeline and Composition`` =
// Pipeline
let isEven x = x % 2 = 0
let input = [1 .. 50]
let result =
input
|> List.filter isEven
|> List.map (fun x -> x * x)
// Composition
let square x = x * x
let double x = x * 2
let print x = printfn "%i" x
print (double (square 5))
let squareDoublePrint = square >> double >> print
squareDoublePrint 5
module Tuples =
let t = (2,4)
let f = fst t
let s = snd t
let (a,b) = t
printfn "%i i %i" a b
let triple = (1,2,3)
let (_, _, x) = triple
printfn "%i" x
module ``Types (basic)`` =
// Abbreviations (IMO: aliases)
type Identifier = string
type Kilograms = float
// Record
type Product = {
Id : Identifier;
Name : string;
Weight : Kilograms;
}
let iron = {Id = "12.XE.324"; Name = "Iron Irony"; Weight = 2.23}
let otherIron = {iron with Name = "Sad Iron"}
let f (x : string) : string =
x.Replace('ż', 'p')
f "duża"
module PatternMatching =
let rec fib n =
match n with
| 0 -> 1
| 1 -> 1
| n -> (fib (n - 1)) + (fib (n - 2))
let rec fib2 = function
| 0 -> 1
| 1 -> 1
| n -> (fib (n - 1)) + (fib (n - 2))
// Tuple
let (a, b, c) = (1, 2, 3)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment