From 2e86fefe6f15877e8c46a45411a9cbd04b26e2e3 Mon Sep 17 00:00:00 2001 From: Eduardo Julian Date: Tue, 5 Dec 2017 22:55:11 -0400 Subject: - WIP: Caching. --- stdlib/source/lux/data/coll/dict.lux | 14 +++++++++++--- stdlib/source/lux/io.lux | 7 ++++++- stdlib/source/lux/world/file.lux | 20 +++++++++++++------- 3 files changed, 30 insertions(+), 11 deletions(-) (limited to 'stdlib') 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 Functor Monoid] [array "array/" Functor Fold]) [bit] @@ -212,7 +212,7 @@ ## Produces the index of a KV-pair within a #Collisions node. (def: (collision-index Hash key colls) (All [k v] (-> (Hash k) k (Collisions k v) (Maybe Index))) - (:: Monad map product.left + (:: maybe.Monad map product.left (array.find+ (function [idx [key' val']] (:: Hash = key key')) colls))) @@ -493,7 +493,7 @@ ## For #Collisions nodes, do a linear scan of all the known KV-pairs. (#Collisions _hash _colls) - (:: Monad map product.right + (:: maybe.Monad map product.right (array.find (|>> product.left (:: Hash = 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) - [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 [ ] [(def: #export ( file) @@ -93,7 +99,7 @@ ( [] (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] -- cgit v1.2.3