view code/ll.lm @ 40:d5ccb66ae98b

Move some basic library code out of dotScanner.lm into separate files now that import:from works
author Michael Pavone <pavone@retrodev.com>
date Sat, 26 Jul 2014 15:29:01 -0700
parents
children
line wrap: on
line source

#{
	length <- :lst {
		len <- []
		while: { not: (lst isInteger?)} do: {
			lst <- lst tail
			len <- len + 1
		}
		len
	}
	
	reverse <- :lst {
		new <- []
		while: { not: (lst isInteger?)} do: {
			new <- (lst value) | new
			lst <- lst tail
		}
		new
	}

	split:at <- :lst :pos {
		first <- []
		i <- 0
		while: { i < pos } do: {
			first <- (lst value) | first
			lst <- lst tail
			i <- i + 1
		}
		#[(reverse: first) lst]
	}
	
	map <- :lst fun {
		new <- []
		while: { not: (lst isInteger?) } do: {
			new <- (fun: (lst value)) | new
			lst <- lst tail
		}
		reverse: new
	}
	
	fold:with <- :lst acc :fun {
		while: { not: (lst isInteger?) } do: {
			acc <- fun: acc (lst value)
			lst <- lst tail
		}
		acc
	}
	
	filter <- :lst pred {
		new <- []
		while: { not: (lst isInteger?) } do: {
			if: (pred: (lst value)) {
				new <- (lst value) | new
			} else: {}
			lst <- lst tail
		}
		reverse: new
	}

	flatten <- :lst {
		fold: lst [] with: :acc el {
			fold: el acc with: :iacc iel {
				iel | iacc
			}
		}
	}
}