view extendlib.rhope @ 75:0083b2f7b3c7

Partially working implementation of List. Modified build scripts to allow use of other compilers. Fixed some bugs involving method implementations on different types returning different numbers of outputs. Added Fold to the 'builtins' in the comipler.
author Mike Pavone <pavone@retrodev.com>
date Tue, 06 Jul 2010 07:52:59 -0400
parents d0ce696786cc
children
line wrap: on
line source


Val[in:out]
{
	out <- in
}

_Map[list,index,worker:out]
{
	newval <- [
		[worker]Do[  
			[()]Append[ [list]Index[index] ]
		]
	]Index[0]
	
	[list]Next[index]
	{
		out <- [_Map[list, ~, worker]]Set[index, newval]
	}{
		out <- [list]Set[index, newval]
	}
}

Map[list,worker:out]
{
	[list]First
	{
		out <- _Map[list, ~, worker]
	}{
		out <- list
	}
}

_SMap[list,index,worker:out]
{
	newval <- [
		[worker]Do[  
			[()]Append[ [list]Index[index] ]
		]
	]Index[0]
	
	[list]Next[index]
	{
		out <- _SMap[[list]Set[index, newval], ~, worker]
	}{
		out <- [list]Set[index, newval]
	}
}

SMap[list,worker:out]
{
	[list]First
	{
		out <- _SMap[list, ~, worker]
	}{
		out <- list
	}
}

_Key Value Map[list,index,newlist,worker:out]
{
	[worker]Do[
		[[()]Append[ [list]Index[index] ]]Append[index]
	]
	{
		newval <- [~]Index[0]
		newkey <- [~]Index[1]
	}
	
	next <- [newlist]Set[newkey, newval]
	
	[list]Next[index]
	{
		out <- _Key Value Map[list, ~, next, worker]
	}{
		out <- Val[next]
	}
}

New Like@List[in:out]
{
	out <- ()
}

New Like@Dictionary[in:out]
{
	out <- Dictionary[]
}

Key Value Map[list,worker:out]
{
	[list]First
	{
		out <- _Key Value Map[list, ~, New Like[list], worker]
	}{
		out <- New Like[list]
	}
}

To String@String[string:out]
{
	out <- string
}

Empty@String[string:out]
{
	out <- ""
}

In[needle,haystack:found?]
{
	[haystack]Get DString[To String[needle]]
	{
		found? <- Yes
	} {} {} {
		found? <- No
	}
}

Left Trim[string,trim:trimmed]
{
	If[ [[string]Length] > [0] ]
	{
		first,rest <- [string]Slice[1]
		If[ [first]In[trim] ]
		{
			trimmed <- Left Trim[rest, trim]
		}{
			trimmed <- string
		}
	}{
		trimmed <- string
	}
}

Right Trim[string,trim:trimmed]
{
	If[ [[string]Length] > [0] ]
	{
		rest,last <- [string]Slice[ [[string]Length] - [1]]
		If[ [last]In[trim] ]
		{
			trimmed <- Right Trim[rest, trim]
		}{
			trimmed <- string
		}
	}{
		trimmed <- string
	}
}

Trim[string,trim:trimmed]
{
	left <- Left Trim[string, trim]
	trimmed <- Right Trim[left, trim]
}

Max[a,b:max]
{
	If[[a] > [b]]
	{
		max <- a
	}{
		max <- b
	}
}

Min[a,b:min]
{
	If[[a] < [b]]
	{
		min <- a
	}{
		min <- b
	}
}

Count Substring[string,substring:out]
{
	out <- Max[[[[string]Split[substring]]Length] - [1], 0]
}

_Key Value Join[dict,key,key sep,val sep,string:out]
{
	new string <- [[[string]Append[key]]Append[key sep]]Append[ [dict]Index[key] ]
	[dict]Next[key]
	{
		out <- _Key Value Join[dict, ~, key sep, val sep, [new string]Append[val sep]]
	}{
		out <- Val[new string]
	}
}

Key Value Join[dict,key sep,val sep:out]
{
	[dict]First
	{
		out <- _Key Value Join[dict, ~, key sep, val sep, ""]
	}{
		out <- ""
	}
}

_Combine[source,dest,key:out]
{
	new dest <- [dest]Set[key, [source]Index[key]]
	[source]Next[key]
	{
		out <- _Combine[source, new dest, ~]
	}{
		out <- Val[new dest]
	}
}

Combine[source,dest:out]
{
	[source]First
	{
		out <- _Combine[source, dest, ~]
	}{
		out <- dest
	}
}

_Fold[list,index,current,worker:out]
{
	newval <- [
		[worker]Do[  
			[[[()]Append[ current ]]Append[ [list]Index[index] ]]Append[index]
		]
	]Index[0]
	
	[list]Next[index]
	{
		out <- _Fold[list, ~, newval, worker]
	}{
		out <- Val[newval]
	}
}

Fold[worker,start,list:out]
{
	[list]First
	{
		out <- _Fold[list, ~, start, worker]
	}{
		out <- start
	}
}

_Zip[left,lindex,right,rindex,outlist:out]
{
	nlist <- [outlist]Append[
		[[()]Append[ [left]Index[lindex] ]]Append[ [right]Index[rindex] ]
	]
	nlindex <- [left]Next[lindex]
	{
		[right]Next[rindex]
		{
			out <- _Zip[left,nlindex,right,~,nlist]
		}{
			out <- Val[nlist]
		}
	}{
		out <- Val[nlist]
	}
}

Zip[left,right:out]
{
	lindex <- [left]First
	{
		Print[~]
		[right]First
		{
			Print[~]
			out <- _Zip[left,lindex,right,~,()]
		}{
			out <- ()
		}
	}{
		out <- ()
	}
}

_Dict Split[dict,entry,index,keydelim:out]
{
	parts <- [entry]Split[keydelim]
	out <- [dict]Set[[parts]Index[0],[parts]Index[1]]
}

Dict Split[string,keydelim,entrydelim:out]
{
	out <- Fold[["_Dict Split"]Set Input[3, keydelim], Dictionary[], [string]Split[entrydelim]] 
}

Previous@List[list,index:prev index,not found]
{
	prev <- [index] - [1]
	If[[prev] < [0]]
	{
		not found <- list
	}{
		[list]Index[prev]
		{
			prev index <- Val[prev]
		}{
			prev index, not found <- [list]Previous[prev]
		}
	}
}

Last@List[list:out,not found]
{
	out, not found <- [list]Previous[[list]Length]
}

_Reverse Fold[list,index,start,worker:out]
{
	newval <- [
		[worker]Do[  
			[[[()]Append[ start ]]Append[ [list]Index[index] ]]Append[index]
		]
	]Index[0]
	
	[list]Previous[index]
	{
		out <- _Reverse Fold[list, ~, newval, worker]
	}{
		out <- Val[newval]
	}
}

Reverse Fold[worker,start,list:out]
{
	[list]Last
	{
		out <- _Reverse Fold[list, ~, start, worker]
	}{
		out <- list
	}
}

_Join[list,delim,current,index:out]
{
	[list]Next[index]
	{
		out <- _Join[list, delim, [[current]Append[delim]]Append[[list]Index[~]], ~]
	}{
		out <- current
	}
}

Join[list,delim:out]
{
	[list]First
	{
		out <- _Join[list, delim, [list]Index[~], ~]
	}{
		out <- ""
	}
}

Replace[string,find,replace:replaced]
{
	replaced <- [[string]Split[find]]Join[replace]
}

Concatenate[left,right:out]
{
	out <- Fold[["Append"]<String@Worker, left, right]
}

Starts With[thing,starts with:out]
{
	out <- [[thing]Slice[[starts with]Length]] = [starts with]
}

Ends With[thing,ends with:out]
{
	,compare <- [thing]Slice[ [[thing]Length] - [[ends with]Length] ]
	out <- [compare] = [ends with]
}

As List@String[string:list]
{
	list <- [()]Append[string]
}

As List@List[in:out]
{
	out <- in
}

_Filter[list,index,worker,destlist:out]
{
	filter? <- [
		[worker]Do[  
			[()]Append[ [list]Index[index] ]
		]
	]Index[0]
	If[filter?]
	{
		newlist <- [destlist]Append[[list]Index[index]]
	}{
		newlist <- destlist
	}
	
	[list]Next[index]
	{
		out <- _Filter[list, ~, worker, newlist]
	}{
		out <- Val[newlist]
	}
}

Filter[list,worker:out]
{
	[list]First
	{
		out <- _Filter[list, ~, worker, ()]
	}{
		out <- list
	}
}

Pop@List[list:out]
{
	[list]Last
	{
		out <- [list]Remove[~]
	}{
		out <- list
	}
}

Peek@List[list:out,empty]
{
	[list]Last
	{
		out <- [list]Index[~]
	}{
		empty <- list
	}
}

Contains[haystack,needle:out]
{
	[haystack]Get DString[needle]
	{
		out <- Yes
	} {} {} {
		out <- No
	}
}

_Find[haystack,needle,index:outindex,notfound]
{
	If[[[haystack]Index[index]] = [needle]]
	{
		outindex <- index
	}{
		[haystack]Next[index]
		{
			outindex,notfound <- _Find[haystack,needle,~]
		}{
			notfound <- needle
		}	
	}
}

Find[haystack,needle:index,not found]
{
	[haystack]First
	{
		index,not found <- _Find[haystack, needle, ~]
	}{
		not found <- needle
	}

}

Get Pretty Print Value[value:print,index,print indent,done,out value]
{
	If[[Type Of[value]] = ["List"]]
	{
		out value <- value
		list <- value
		object <- value
	}{
		If[[Type Of[value]] = ["Dictionary"]]
		{
			out value <- value
			list <- value
			object <- value
		}{
			If[[Type Of[value]] = ["String"]]
			{
				out value <- value
				print <- value
				done <- 1
			}{
				If[[Type Of[value]] = ["Whole Number"]]
				{
					out value <- value
					print <- value
					done <- 1
				}{
					If[[Type Of[value]] = ["Yes No"]]
					{
						out value <- value
						print <- value
						done <- 1
					}{
						If[[Type Of[value]] = ["Real Number"]]
						{
							out value <- value
							print <- value
							done <- 1
						}{
							object <- value
							fieldlist <- [Blueprint Of[value]]Get Fields
							[fieldlist]First
							{
								list <- _Object to Dict[value, fieldlist, ~, Dictionary[]]
								out value <- Val[list]
							}{
								out value <- value
								done <- 1
							}
						}
					}
				}
			}
			
		}
	}
	print <- Type Of[object]
	index <- [list]First {}
	{
		print indent <- "{Empty}"
	}
	
}

Pretty Print Helper[list,tabs,index:out]
{
	newtabs <- [tabs]Append["    "]
	print,new index,indented,done,value <- Get Pretty Print Value[[list]Index[index]]
	Print[ [[[tabs]Append[index]]Append[": "]]Append[print] ]
	{
		done <- Pretty Print Helper[value,newtabs ,new index]
		done <- Print[[newtabs]Append[indented]]
		
		Val[done]
		{
			[list]Next[index]
			{
				out <- Pretty Print Helper[list, tabs, ~]
			}{
				out <- 1
			}
		}
	}
	
}

Pretty Print[toprint,tabs:out]
{
	newtabs <- [tabs]Append["    "]
	,index,indented,,value <- Get Pretty Print Value[toprint]
	{
		Print[[tabs]Append[~]]
		{
			Pretty Print Helper[value,newtabs ,index]
			Print[[newtabs]Append[indented]]
		}
	}
	out <- 1
}

_Object to Dict[object,field list,index,dict:out]
{
	field <- [field list]Index[index]
	[object]Get Field[field]
	{
		nextdict <- [dict]Set[field, ~]
	}{
		nextdict <- dict
	}
	[field list]Next[index]
	{
		out <- _Object to Dict[object, field list, ~, nextdict]
	}{
		out <- Val[nextdict]
	}
}

_Keys[list,val,key:out]
{
	out <- [list]Append[key]
}

Keys[container:out]
{
	out <- Fold["_Keys", (), container]
}

And[left,right:out]
{
	,out <- If[left]
	{
		out,out <- If[right]
	}
}

Or[left,right:out]
{
	out <- If[left] {}
	{
		out,out <- If[right]
	}
}

After[text,prefix:after,not found]
{
	If[[text]Starts With[prefix]]
	{
		,after <- [text]Slice[[prefix]Length]
	}{
		not found <- text
	}
}
 
Blueprint Range
{
	Start
	End
}

Range[start,end:out]
{
	out <- [[Build["Range"]]Start <<[start]]End <<[end]
}

First@Range[range:out,none]
{
	If[[[range]Start >>] < [[range]End >>]]
	{
		out <- [range]Start >>
	}{
		none <- range
	}
}

Next@Range[range,val:out,done]
{
	next <- [val]+[1]
	If[[next] < [[range]End >>]]
	{
		out <- Val[next]
	}{
		done <- range
	}
}

Index@Range[range,index:out,notfound]
{
	If[[index] < [[range]End >>]]
	{
		If[[index] < [[range]Start >>]]
		{
			notfound <- index
		}{
			out <- index
		}
	}{
		notfound <- index
	}
}

Tail[list,start:out]
{
	newlist <- New Like[list]
	[list]Index[start]
	{
		out <- _Fold[list, start, newlist, "Append"]
	}{
		out <- Val[newlist]
	}
}

_Collection =[col1,key1,col2,key2:out]
{
	,out <- If[[key1]=[key2]]
	{
		,out <- If[[[col1]Index[key1]] = [[col2]Index[key2]]]
		{
			nkey1 <- [col1]Next[key1]
			{
				[col2]Next[key2]
				{
					out <- _Collection =[col1,nkey1,col2,~]
				}{
					out <- No
				}
			}{
				[col2]Next[key2]
				{
					out <- No
				}{
					out <- Yes
				}
			}
		}
	}
}

Collection =[col1,col2:out]
{
	first1 <- [col1]First
	{
		[col2]First
		{
			out <- _Collection =[col1, first1, col2, ~]
		}{
			out <- No
		}
	}{
		[col2]First
		{
			out <- No
		}{
			out <- Yes
		}
	}
}

=@List[list1,list2:out]
{
	,out <- If[[[list1]Length >>] = [[list2]Length >>]]
	{
		out <- Collection =[list1,list2]
	}
}

=@Dictionary[list1,list2:out]
{
	,out <- If[[[list1]Length >>] = [[list2]Length >>]]
	{
		out <- Collection =[list1,list2]
	}
}