Revision: 411
http://pure-lang.svn.sourceforge.net/pure-lang/?rev=411&view=rev
Author: jspitz
Date: 2008-07-07 13:46:37 -0700 (Mon, 07 Jul 2008)
Log Message:
-----------
Add slicing operation
Modified Paths:
--------------
pure/trunk/examples/array.pure
pure/trunk/examples/dict.pure
Modified: pure/trunk/examples/array.pure
===================================================================
--- pure/trunk/examples/array.pure 2008-07-07 14:22:07 UTC (rev 410)
+++ pure/trunk/examples/array.pure 2008-07-07 20:46:37 UTC (rev 411)
@@ -37,6 +37,11 @@
a!i return ith member of a
a!(i,j) two-dimensional subscript
+ a!!is slicing (get a list of values from a list
+ indices
+ a!!ijs slicing of two-dimensional array (from a given
+ list of pairs (i, j):...:[])
+
null a tests whether a is the empty array
members a list of values stored in a
members2 a list of members in a two-dimensional array
@@ -100,6 +105,14 @@
// get value by indices from two-dimensional array
x@(Array _)!(i::int, j::int) = x!i!j;
+// slicing (get list of values from list of indices)
+a@(Array _)!!is@(_::int:_) = [a!i; i = is; (i >= 0) && (i < (#a))];
+
+// slicing of two-dimensional array
+a@(Array _)!!ijs@((_::int, _::int):_)
+ = [a!(i, j); (i, j) = ijs; (i >= 0) && (i < (#a))
+ && (j >= 0) && (j < (#(a!i)))];
+
// check for an empty array
null (Array nil) = 1;
null (Array _) = 0;
Modified: pure/trunk/examples/dict.pure
===================================================================
--- pure/trunk/examples/dict.pure 2008-07-07 14:22:07 UTC (rev 410)
+++ pure/trunk/examples/dict.pure 2008-07-07 20:46:37 UTC (rev 411)
@@ -52,6 +52,8 @@
#d size of dict or hdict d
d!x: get value from d by key x
+d!!xs slicing (get a list of values
+ from a list of keys)
null d tests whether d is the empty dict or hdict
member d x tests whether d contains member with key x
@@ -421,7 +423,9 @@
vals (Hdict d) = vals d
with
vals nil = [];
- vals (bin _ xys _ d1 d2) = vals d1 + map (\(_ => val) -> val) xys + vals d2
+ vals (bin _ xys _ d1 d2) = vals d1 +
+ map (\(_ => val) -> val) xys +
+ vals d2
end;
// get a value by key from dict or hdict
@@ -453,6 +457,47 @@
lookupk ( _ :xys) x = lookupk xys x
end;
+// slicing (get list of values from list of keys)
+(Dict d)!!xs = slice d [] xs
+with
+ slice d ys (x:xs) = slice d
+ (case mbr of nil = ys;
+ (nonil y) = (y:ys) end) xs
+ when
+ mbr = d!x
+ end;
+ slice d ys [] = reverse ys;
+
+ nil!_ = nil;
+ (bin x::int y _ d1 d2)!x1::int |
+ (bin x::string y _ d1 d2)!x1::string |
+ (bin x y _ d1 d2)!x1
+ = d1!x1 if x1 < x;
+ = d2!x1 if x1 > x;
+ = nonil y
+end;
+
+(Hdict d)!!xs = slice d [] xs
+with
+ slice d ys (x:xs) = slice d
+ (case mbr of nil = ys;
+ (nonil y) = (y:ys) end) xs
+ when
+ mbr = lookup d (hash x) x
+ end;
+ slice d ys [] = reverse ys;
+
+ lookup nil _ _ = nil;
+ lookup (bin k::int xys _ d1 d2) k1::int x1
+ = lookup d1 k1 x1 if k > k1;
+ = lookup d2 k1 x1 if k < k1;
+ = lookupk xys x1;
+
+ lookupk [] _ = nil;
+ lookupk ((xa => y):_ ) xb = nonil y if xa === xb;
+ lookupk ( _ :xys) x = lookupk xys x
+end;
+
// curried version of insert for dict and hdict
update d@(Dict _) x::int y |
update d@(Dict _) x::string y |
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|