Mercurial > repos > rhope
diff extendlib.rhope @ 0:76568becd6d6
Rhope Alpha 2a source import
author | Mike Pavone <pavone@retrodev.com> |
---|---|
date | Tue, 28 Apr 2009 23:06:07 +0000 |
parents | |
children | b3f71490858c |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/extendlib.rhope Tue Apr 28 23:06:07 2009 +0000 @@ -0,0 +1,526 @@ + +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 + } +} + +_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 <- New@Dictionary[] +} + +Key Value Map[list,worker:out] +{ + [list]First + { + out <- _Key Value Map[list, ~, New Like[list], worker] + }{ + out <- New Like[list] + } +} + +In[needle,haystack:found?] +{ + [haystack]Get DString[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 + } +} + +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 + } +} + +_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], New@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, ~, New@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", New@List[], container] +} +