1 people like it.

JessiTron.fsx

An F# implementation of Jessica Kerr's blog post on imperative to functional. http://blog.jessitron.com/2013/01/from-imperative-to-data-flow-to.html

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
15: 
16: 
17: 
18: 
19: 
20: 
21: 
22: 
23: 
24: 
25: 
26: 
27: 
28: 
29: 
30: 
31: 
32: 
33: 
34: 
35: 
// F# version of Jessica Kerr's blog post http://blog.jessitron.com/2013/01/from-imperative-to-data-flow-to.html
#load "/jbuedel/Attempt.fsx"

open System
open System.IO
open Attempt

let openFile filename = 
    try 
      System.IO.File.OpenRead filename |> succeed
    with 
    | _ -> fail
    
let readFirstLine (file:TextReader) =
    try 
        file.ReadLine() |> succeed
    with 
    | _ -> fail
    
let getSecret (line:string) =
    if line = null || line.Contains("'") = false then
        fail
    else
        line.Substring(1 + line.IndexOf("'")) |> succeed
    
let readSecret stream =
    attempt {
        let! line = readFirstLine stream
        let! secret = getSecret line
        return secret
    } |> (fun x -> x())

match (readSecret (new StringReader("hello there 'kind sir"))) with
    | None  -> printfn "%s" " Secret not found. "
    | Some(s) -> printfn "Secret is [%s]." s
namespace System
namespace System.IO
val openFile : filename:string -> 'a

Full name: Script.openFile
val filename : string
type File =
  static member AppendAllLines : path:string * contents:IEnumerable<string> -> unit + 1 overload
  static member AppendAllText : path:string * contents:string -> unit + 1 overload
  static member AppendText : path:string -> StreamWriter
  static member Copy : sourceFileName:string * destFileName:string -> unit + 1 overload
  static member Create : path:string -> FileStream + 3 overloads
  static member CreateText : path:string -> StreamWriter
  static member Decrypt : path:string -> unit
  static member Delete : path:string -> unit
  static member Encrypt : path:string -> unit
  static member Exists : path:string -> bool
  ...

Full name: System.IO.File
File.OpenRead(path: string) : FileStream
val readFirstLine : file:TextReader -> 'a

Full name: Script.readFirstLine
val file : TextReader
type TextReader =
  inherit MarshalByRefObject
  member Close : unit -> unit
  member Dispose : unit -> unit
  member Peek : unit -> int
  member Read : unit -> int + 1 overload
  member ReadBlock : buffer:char[] * index:int * count:int -> int
  member ReadLine : unit -> string
  member ReadToEnd : unit -> string
  static val Null : TextReader
  static member Synchronized : reader:TextReader -> TextReader

Full name: System.IO.TextReader
TextReader.ReadLine() : string
val getSecret : line:string -> 'a

Full name: Script.getSecret
val line : string
Multiple items
val string : value:'T -> string

Full name: Microsoft.FSharp.Core.Operators.string

--------------------
type string = String

Full name: Microsoft.FSharp.Core.string
String.Contains(value: string) : bool
String.Substring(startIndex: int) : string
String.Substring(startIndex: int, length: int) : string
String.IndexOf(value: string) : int
String.IndexOf(value: char) : int
String.IndexOf(value: string, comparisonType: StringComparison) : int
String.IndexOf(value: string, startIndex: int) : int
String.IndexOf(value: char, startIndex: int) : int
String.IndexOf(value: string, startIndex: int, comparisonType: StringComparison) : int
String.IndexOf(value: string, startIndex: int, count: int) : int
String.IndexOf(value: char, startIndex: int, count: int) : int
String.IndexOf(value: string, startIndex: int, count: int, comparisonType: StringComparison) : int
val readSecret : stream:'a -> 'b

Full name: Script.readSecret
val stream : 'a
val x : (unit -> 'b)
Multiple items
type StringReader =
  inherit TextReader
  new : s:string -> StringReader
  member Close : unit -> unit
  member Peek : unit -> int
  member Read : unit -> int + 1 overload
  member ReadLine : unit -> string
  member ReadToEnd : unit -> string

Full name: System.IO.StringReader

--------------------
StringReader(s: string) : unit
union case Option.None: Option<'T>
val printfn : format:Printf.TextWriterFormat<'T> -> 'T

Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.printfn
union case Option.Some: Value: 'T -> Option<'T>
val s : string
Raw view New version

More information

Link:http://fssnip.net/gC
Posted:4 years ago
Author:jbuedel
Tags: tryfsharp