Skip to content

Commit

Permalink
filtermap and chooe
Browse files Browse the repository at this point in the history
  • Loading branch information
s952163 committed Apr 29, 2017
1 parent 80e7b15 commit c5d4610
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 2 deletions.
49 changes: 49 additions & 0 deletions fsharpexpert.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
open System
open System.Windows.Forms


type RandomTicker(approxInterval) =
let timer = new Timer()
let rnd = new System.Random(99)
let tickEvent = new Event<int> ()

let chooseInterval() : int =
approxInterval + approxInterval / 4 - rnd.Next(approxInterval / 2)

do timer.Interval <- chooseInterval()

do timer.Tick.Add(fun args ->
let interval = chooseInterval()
tickEvent.Trigger interval;
timer.Interval <- interval)

member x.RandomTick = tickEvent.Publish
member x.Start() = timer.Start()
member x.Stop() = timer.Stop()
interface IDisposable with member x.Dispose() = timer.Dispose()

let rt = new RandomTicker(1000);;
rt.RandomTick.Add(fun nextInterval -> printfn "Tick, next = %A" nextInterval);;
rt.Start()
rt.Stop()

let xs = [1..10]
xs
|> List.filter (fun x -> x % 2 = 0)
|> List.map (fun x -> x * 2)

let filterMap f m = (List.filter f >> List.map m)
xs
|> filterMap (fun x -> x % 2 = 0) (fun x -> x * 2)

xs
|> List.choose (fun x ->
match x with
| x when x % 2 = 0 -> Some (x * 2)
| _ -> None )


xs
|> List.choose (function
| x when x % 2 = 0 -> Some (x * 2)
| _ -> None )
8 changes: 6 additions & 2 deletions samplecode.fsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ type MyRec = {
id : int
}

let employee1 = {name="John Doe"; id=99}
let employee1 = {MyRec.name="John Doe"; id=99}
let employee2 = {name="John Doe"; id=99}
let check = employee1 = employee2

Expand All @@ -35,4 +35,8 @@ printfn "%A" dtAndNum

module TestModule =
let newfunc x =
x + "a"
x + "a"

let f x (g : unit -> unit) : unit =
printf x

Binary file added slides/images/Suave1.PNG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit c5d4610

Please sign in to comment.