Skip to content

Instantly share code, notes, and snippets.

@undeadcat
Created August 17, 2015 20:07
Show Gist options
  • Save undeadcat/2f35c8350a5d941de891 to your computer and use it in GitHub Desktop.
Save undeadcat/2f35c8350a5d941de891 to your computer and use it in GitHub Desktop.
Streams
open System
type Stream<'t> =
| Empty
| Stream of 't * (unit -> Stream<'t>)
let takeList count stream =
let rec inner remain remainCount tail =
match (remain, remainCount) with
| (Empty, _) -> tail
| (Stream(item, rest), remainCount) ->
if remainCount > 0 then inner (rest()) (remainCount - 1) (item :: tail)
else tail
(inner stream count []) |> List.rev
let toList stream =
let rec inner remain tail =
match remain with
| Empty -> tail
| Stream(item, stream) -> inner (stream()) (item :: tail)
inner stream [] |> List.rev
let rec take count stream =
match stream with
| Empty -> Empty
| Stream(item, stream) ->
Stream(item,
fun () ->
if count > 1 then take (count - 1) (stream())
else Empty)
let ints =
let rec inner (start) = Stream(start, fun () -> inner (start + 1))
inner 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment