Created
November 28, 2012 15:14
-
-
Save raimohanska/4161884 to your computer and use it in GitHub Desktop.
Array helpers in Roy, Part II
This file contains hidden or 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
let jsarray xs = | |
let plusOne = Array.prototype.concat.apply [1] xs | |
let jsarr = Array.apply null plusOne | |
jsarr.slice 1 | |
let always x = (\() -> x) | |
let length xs = (jsarray xs).length | |
let empty xs = (length xs) == 0 | |
let concat xs ys = (jsarray xs).concat ys | |
let cons x xs = concat [x] xs | |
let head xs = xs@0 | |
let tail xs = (jsarray xs).slice 1 | |
let times n x = map (always x) (range 1 n) | |
let map f xs = | |
if empty xs then | |
[] | |
else | |
cons (f (head xs)) (map f (tail xs)) | |
let filter f xs = | |
if empty xs then | |
[] | |
else | |
if f (head xs) then | |
cons (head xs) (filter f (tail xs)) | |
else | |
filter f (tail xs) | |
let range start end = | |
if start > end then | |
[] | |
else | |
cons start (range (start+1) end) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This time a working version :) The jsarray thing is a terrible hack but it's the best I've come up so far.
Anyways, this seems to be a working set of array utils in Roy:
length xs
empty xs
head xs
tail xs
map f xs
filter f xs
concat xs ys
cons x ys
times n x
range start end