Created
August 17, 2015 20:07
-
-
Save undeadcat/2f35c8350a5d941de891 to your computer and use it in GitHub Desktop.
Streams
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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