diff options
author | Eduardo Julian | 2017-12-05 22:55:11 -0400 |
---|---|---|
committer | Eduardo Julian | 2017-12-05 22:55:11 -0400 |
commit | 2e86fefe6f15877e8c46a45411a9cbd04b26e2e3 (patch) | |
tree | c95f8c1391b907a3e9076f4eadb831aa274cfbea /stdlib | |
parent | 0b87f118978e9971828d2c9ccabe685b0c5e4c35 (diff) |
- WIP: Caching.
Diffstat (limited to 'stdlib')
-rw-r--r-- | stdlib/source/lux/data/coll/dict.lux | 14 | ||||
-rw-r--r-- | stdlib/source/lux/io.lux | 7 | ||||
-rw-r--r-- | stdlib/source/lux/world/file.lux | 20 |
3 files changed, 30 insertions, 11 deletions
diff --git a/stdlib/source/lux/data/coll/dict.lux b/stdlib/source/lux/data/coll/dict.lux index e00cbb17f..1091100b6 100644 --- a/stdlib/source/lux/data/coll/dict.lux +++ b/stdlib/source/lux/data/coll/dict.lux @@ -2,7 +2,7 @@ lux (lux (control hash [eq #+ Eq]) - (data maybe + (data [maybe] (coll [list "list/" Fold<List> Functor<List> Monoid<List>] [array "array/" Functor<Array> Fold<Array>]) [bit] @@ -212,7 +212,7 @@ ## Produces the index of a KV-pair within a #Collisions node. (def: (collision-index Hash<k> key colls) (All [k v] (-> (Hash k) k (Collisions k v) (Maybe Index))) - (:: Monad<Maybe> map product.left + (:: maybe.Monad<Maybe> map product.left (array.find+ (function [idx [key' val']] (:: Hash<k> = key key')) colls))) @@ -493,7 +493,7 @@ ## For #Collisions nodes, do a linear scan of all the known KV-pairs. (#Collisions _hash _colls) - (:: Monad<Maybe> map product.right + (:: maybe.Monad<Maybe> map product.right (array.find (|>> product.left (:: Hash<k> = key)) _colls)) )) @@ -588,6 +588,14 @@ (#.Some val) (put key (f val) dict))) +(def: #export (update~ key default f dict) + {#.doc "Transforms the value located at key (if available), using the given function."} + (All [k v] (-> k v (-> v v) (Dict k v) (Dict k v))) + (put key + (f (maybe.default default + (get key dict))) + dict)) + (def: #export size (All [k v] (-> (Dict k v) Nat)) (|>> product.right size')) diff --git a/stdlib/source/lux/io.lux b/stdlib/source/lux/io.lux index 8eaeaae63..5e1f2e59e 100644 --- a/stdlib/source/lux/io.lux +++ b/stdlib/source/lux/io.lux @@ -2,7 +2,8 @@ lux (lux (control [functor #+ Functor] [applicative #+ Applicative] - [monad #+ do Monad]) + [monad #+ do Monad] + ["ex" exception #+ Exception]) (data ["e" error #+ Error] (coll [list])))) @@ -79,3 +80,7 @@ (def: #export (fail error) (All [a] (-> Text (Process a))) (io (#e.Error error))) + +(def: #export (throw exception message) + (All [a] (-> Exception Text (Process a))) + (io (#e.Error (exception message)))) diff --git a/stdlib/source/lux/world/file.lux b/stdlib/source/lux/world/file.lux index 1c295da08..b173fa5ba 100644 --- a/stdlib/source/lux/world/file.lux +++ b/stdlib/source/lux/world/file.lux @@ -10,17 +10,18 @@ [io #+ Process] [host])) -(exception: Could-Not-Read-All-Data) +(exception: #export Could-Not-Read-All-Data) +(exception: #export Not-A-Directory) (type: #export File Text) (host.import #long java/io/File (new [String]) (exists [] #io #try boolean) - (mkdir [] #io #try boolean) + (mkdirs [] #io #try boolean) (delete [] #io #try boolean) (length [] #io #try long) - (listFiles [] #io #try (Array java/io/File)) + (listFiles [] #io #try #? (Array java/io/File)) (getAbsolutePath [] #io #try String) (renameTo [java/io/File] #io #try boolean) (isFile [] #io #try boolean) @@ -83,9 +84,14 @@ (def: #export (files dir) (-> File (Process (List File))) (do (E.ErrorT io.Monad<IO>) - [files (java/io/File::listFiles [] (java/io/File::new dir))] - (monad.map @ (java/io/File::getAbsolutePath []) - (array.to-list files)))) + [?files (java/io/File::listFiles [] (java/io/File::new dir))] + (case ?files + (#.Some files) + (monad.map @ (java/io/File::getAbsolutePath []) + (array.to-list files)) + + #.None + (io.throw Not-A-Directory dir)))) (do-template [<name> <method>] [(def: #export (<name> file) @@ -93,7 +99,7 @@ (<method> [] (java/io/File::new file)))] [exists? java/io/File::exists] - [make-dir java/io/File::mkdir] + [make-dir java/io/File::mkdirs] [delete java/io/File::delete] [file? java/io/File::isFile] [directory? java/io/File::isDirectory] |