changeset 343:21e20c9bb2ba

Added range module and sample
author Michael Pavone <pavone@retrodev.com>
date Sun, 05 Apr 2015 23:35:05 -0700
parents 884cd5d54c0f
children 7de6ac24eb64
files modules/range.tp samples/range.tp
diffstat 2 files changed, 75 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/modules/range.tp	Sun Apr 05 23:35:05 2015 -0700
@@ -0,0 +1,64 @@
+#{
+	from:to:by <- :_from :_to :_by {
+		#{
+			from <- { _from }
+			to <- { _to }
+			by <- { _by }
+			
+			legnth <- { (_to - _from) / _by }
+			
+			get <- :idx { _to + idx * _by}
+			
+			array <- {
+				fold: #[] with: :acc el {
+					acc append: el
+				}
+			}
+			
+			list <- {
+				map: :el { el }
+			}
+			
+			map <- :fun {
+				foldr: [] with: :acc el {
+					(fun: el) | acc
+				}
+			}
+			
+			foldr:with <- :acc :fun {
+				cur <- _from + ((_to - _from) / _by) * _by
+				while: { cur >= _from } do: {
+					acc <- fun: acc cur
+				
+					cur <- cur - _by
+				}
+				acc
+			}
+			
+			fold:with <- :acc :fun {
+				cur <- _from
+				while: { cur < _to } do: {
+					acc <- fun: acc cur
+				
+					cur <- cur + _by
+				}
+				acc
+			}
+			
+			foreach <- :fun {
+				idx <- 0
+				cur <- _from
+				res <- cur
+				while: { cur < _to} do: {
+					res <- fun: idx cur
+				
+					idx <- idx + 1
+					cur <- cur + _by
+				}
+				res
+			}
+		}
+	}
+	
+	from:to <- :f :t { from: f to: t by: 1 }
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/samples/range.tp	Sun Apr 05 23:35:05 2015 -0700
@@ -0,0 +1,11 @@
+#{
+	main <- {
+		foreach: (range from: 10 to: 20) :idx el {
+			print: (string: idx) . ": " . el . "\n"
+		}
+		sum <- (range from: 0 to: 100 by: 3) fold: 0 with: :acc el {
+			acc + el
+		}
+		print: "Sum of range from: 0 to: 100 by: 3 is " . sum . "\n"
+	}
+}
\ No newline at end of file