[ create a new paste ] login | about

Link: http://codepad.org/BwzGzeev    [ raw code | fork ]

scwizard - Plain Text, pasted on Jan 20:
# My version:
function {
	std$vector{'1.3', '2.7', '3.9'} of double{}:; named "from";:
	variable std$vector{'7', '8', '9'} of int{}:; named "to";:
	std$copy[] from from; range from.begin[]: to from.end[]:; to to.begin[]:;:
	# The same results could have been produced with:
	# std$copy[] from from; to to;:
	# This is because the default for the range of the input to copy, is the whole thing.
	# And also because the place in the output to begin copying to is by default the beginning.
	# That is if the output container itself is specified instead of an iterator constructed from the output container.
	std$out[string{to}: + string::endl]:
}
takes parameter{string{}:} named "CmdLine";:;
returns int{}:;
throws nothing{}:;
named "EntryPoint";:

# An implementation of copy:
function {
	iterator{CopyTo} named "OutputIter";:
	iterator{~CopyFrom} range CopyRange; named "InputIter";:
	for[CopyRange] do function {
		# This section shows how having words as operators can improve readability.
		~OutputIter := dereference InputIter;
		++OutputIter;
		increment InputIter;
	}:;:
	return OutputIter;
}
takes parameter{^container{}:} named "CopyFrom"; keyword "from";:;
# It is the following line having range{CopyFrom}: instead of range{}: that makes the following illegal:
# std$copy[] from from; range '1' to '5'; to to.begin[]:;:
# If you try that you'll get a compiler error saying that there is no function that takes a range{} as that parameter.
takes parameter{range{CopyFrom}:} named "CopyRange"; keyword "range";:;
# Unlike in C++ here we can say "over a variable container".
# An iterator constructed with a variable container is a more specific type than an iterator constructed with a container. 
takes variable parameter{iterator{} over variable container{}:;:} named "CopyTo"; keyword "to";:;
returns iterator{} over CopyTo;:;
# If ++OutputIter fails due to exceeding its range, or ~OutputIter fails due to being at the end, then OutputIter will be thrown.
throws iterator{} over CopyTo;:;
named "Copy";:

# An implementation of for:
function {
	variable iterator{ForRange} named "ForIterator";:
	# While is a special function. You can't create a loop without a loop.
	while[ForIterator != ForRange.End[]:] loop function {
		ForFunction[]:
		++ForIterator;
	}:;:
}
takes parameter{range{}:} named "ForRange"; keyword "range";:;
takes parameter{function{}:} named "ForFunction"; keyword "do";:;
# Returns the same thing as C++'s for_each.
returns ForFunction;
throws iterator{ForRange}:;
named "For";:


Create a new paste based on this one


Comments: