How to create functional pipes for functions/methods in Groovy? - groovy

I want to create pipes to connect Groovy function (method) calls like its done in other functional languages, such as F#:
let print message =
printf "%s" message
// "Hello World" will be passed as a parameter to the print function
"Hello World" |> print
There is a naive implementation using the or operator:
Object.metaClass.or { it -> it(delegate)}
def print = { msg ->
println msg
}
"Hello World" | print //Hello World
But it only works for functions with 1 parameter. For more parameters, rcurry has to be used:
Object.metaClass.or { it -> it(delegate)}
def print = { msg1, msg2 ->
println msg1 + msg2
}
"Hello World" | print.rcurry('!!!') //Hello World!!!
Is there a way to get rid of the rcurry method and make the Groovy code more similar to F#'s? BTW, this naive implementation only works for Groovy script files. How do I make it work also for class files?
Notice: There are other questions about pipes in Groovy but these are about pipes for shell commands not functions.

It looks like you just want with and using .& to convert methods to Closures, and >> for function composition.
public static String addExclamations(String s) {
return s + "!!!"
}
Closure printUppercase = { String s -> println s.toUpperCase() }
"Hello World".with (this.&addExclamations >> printUppercase)
A real world example I find myself using all the time:
import groovy.json.JsonOutput
[a:[b:'c', d:'e']].with (JsonOutput.&toJson >> JsonOutput.&prettyPrint)
I'm not sure what you're getting at with the multiparameter stuff, because the various curry solutions seem sensible to me, but you've dismissed that... perhaps I can edit this answer if you can give an example of how you would like it to look?

Related

How to catch the output of a compiler error in nim?

I am not sure if this is currently possible (and maybe it is not even advisable), but I would like to be able to catch the output of a compiler error and reuse it in code. An example would be:
type IntOrString = int | string
var s: seq[IntOrString]
this code would not compile with error:
/usercode/in.nim(2, 5) Error: invalid type: 'IntOrString' in this context: 'seq[IntOrString]' for var
I am interested in a way to be able to use the message of this error in the code.
My use case is being able to easily document and discuss compiler errors in nimib. If I were to write a document that shows and discuss the different type of compiler errors catching the message automatically that could be useful (a workaround right now would be to write the code to file and compile with verbosity 0).
It is possible to use the compiler api to catch errors:
import ../compiler/[nimeval, llstream, ast, lineinfos, options], os, strformat
let std = findNimStdLibCompileTime()
let modules = [std, std / "pure", std / "std", std / "core"]
var intr = createInterpreter("script", modules)
intr.registerErrorHook proc(config:ConfigRef,info:TLineInfo,msg:string,severity:Severity) =
raise newException(CatchableError,&"{severity}: {(info.line,info.col)} {msg}")
)
try:
intr.evalScript(llStreamOpen("""type IntOrString = int | string
var s: seq[IntOrString]"""))
except CatchableError as e:
echo e.msg
echo "done"
destroyInterpreter(intr)
outputs:
Error: (2, 4) invalid type: 'IntOrString' in this context: 'seq[IntOrS
tring]' for var
done
Caveat: you can't run runtime code at compile time, for example, trying to run
type Dog = object of RootObj
method speak(i:Dog):string = "woof"
echo Dog().speak()
in the interpreter will give you errors, instead you would have to do something like:
type Dog = object of RootObj
method speak*(i:Dog):string = "woof"
proc newDog*():Dog = discard
let
dog = intr.callRoutine(intr.selectRoutine("newDog"),[])
speech = intr.callRoutine(intr.selectRoutine("speak"),[dog])
if speech.kind == nkStrLit:
echo speech.strval

What's wrong with this groovy for-loop of Closures? [duplicate]

In the context of Jenkins pipelines, I have some Groovy code that's enumerating a list, creating closures, and then using that value in the closure as a key to lookup another value in a map. This appears to be rife with some sort of anomaly or race condition almost every time.
This is a simplification of the code:
def tasks = [:]
for (platformName in platforms) {
// ...
tasks[platformName] = {
def componentUploadPath = componentUploadPaths[platformName]
echo "Uploading for platform [${platformName}] to [${componentUploadPath}]."
// ...
}
tasks.failFast = true
parallel(tasks)
platforms has two values. I will usually see two iterations and two tasks registered and the keys in tasks will be correct, but the echo statement inside the closure indicates that we're just running one of the platforms twice:
14:20:02 [platform2] Uploading for platform [platform1] to [some_path/platform1].
14:20:02 [platform1] Uploading for platform [platform1] to [some_path/platform1].
It's ridiculous.
What do I need to add or do differently?
It's the same issue as you'd see in Javascript.
When you generate the closures in a for loop, they are bound to a variable, not the value of the variable.
When the loop exits, and the closures are run, they will all be using the same value...that is -- the last value in the for loop before it exited
For example, you'd expect the following to print 1 2 3 4, but it doesn't
def closures = []
for (i in 1..4) {
closures << { -> println i }
}
closures.each { it() }
It prints 4 4 4 4
To fix this, you need to do one of two things... First, you could capture the value in a locally scoped variable, then close over this variable:
for (i in 1..4) {
def n = i
closures << { -> println n }
}
The second thing you could do is use groovy's each or collect as each time they are called, the variable is a different instance, so it works again:
(1..4).each { i ->
closures << { -> println i }
}
For your case, you can loop over platforms and collect into a map at the same time by using collectEntries:
def tasks = platforms.collectEntries { platformName ->
[
platformName,
{ ->
def componentUploadPath = componentUploadPaths[platformName]
echo "Uploading for platform [${platformName}] to [${componentUploadPath}]."
}
]
}
Hope this helps!

How to mark non-pure function as pure in Nim

Is there a way to mark non-pure function p as pure? Maybe with some pragma?
I'm using p for debug, and it can't be used inside pure func procs.
playground
proc p(message: string): void = echo message
func purefn: void =
p "track"
purefn()
Error:
/usercode/in.nim(3, 6) Error: 'purefn' can have side effects
Well, for a start you can just use debugEcho instead of echo - it has no side effects (and it's specifically made for use-cases like that).
In other cases you can "lie" to the compiler by doing:
proc p(message: string) =
{.cast(noSideEffect).}:
echo message
func purefn =
p "track"
purefn()
as described in https://nim-lang.org/docs/manual.html#pragmas-nosideeffect-pragma, but I would advise against it.
For your case, you can use debugEcho inside of echo which fakes having no side effects.
Other than that, you can use the {.cast(noSideEffect).} pragma if you're not using echo in your real code:
proc p(message: string): void = echo message
func purefn: void =
{.cast(noSideEffect).}:
p "track"
purefn()

Difference between String and StaticString

I was browsing the docs, and I found StaticString. It states:
An simple string designed to represent text that is "knowable at compile-time".
I originally thought that String has the same behaviour as NSString, which is known at compile time, but it looks like that I was wrong. So my question is when should we use StaticString instead of a String, and is the only difference is that StaticString is known at compile-time?
One thing I found is
var a: String = "asdf" //"asdf"
var b: StaticString = "adsf" //{(Opaque Value), (Opaque Value), (Opaque Value)}
sizeofValue(a) //24
sizeofValue(b) //17
So it looks like StaticString has a little bit less memory footprint.
It appears that StaticString can hold string literals. You can't assign a variable of type String to it, and it can't be mutated (with +=, for example).
"Knowable at compile time" doesn't mean that the value held by the variable will be determined at compile time, just that any value assigned to it is known at compile time.
Consider this example which does work:
var str: StaticString
for _ in 1...10 {
switch arc4random_uniform(3) {
case 0: str = "zero"
case 1: str = "one"
case 2: str = "two"
default: str = "default"
}
print(str)
}
Any time you can give Swift more information about how a variable is to be used, it can optimize the code using it. By restricting a variable to StaticString, Swift knows the variable won't be mutated so it might be able to store it more efficiently, or access the individual characters more efficiently.
In fact, StaticString could be implemented with just an address pointer and a length. The address it points to is just the place in the static code where the string is defined. A StaticString doesn't need to be reference counted since it doesn't (need to) exist in the heap. It is neither allocated nor deallocated, so no reference count is needed.
"Knowable at compile time" is pretty strict. Even this doesn't work:
let str: StaticString = "hello " + "world"
which fails with error:
error: 'String' is not convertible to 'StaticString'
StaticString is knowable at compile time. This can lead to optimizations. Example:
EDIT: This part doesn't work, see edit below
Suppose you have a function that calculates an Int for some String values for some constants that you define at compile time.
let someString = "Test"
let otherString = "Hello there"
func numberForString(string: String) -> Int {
return string.stringValue.unicodeScalars.reduce(0) { $0 * 1 << 8 + Int($1.value) }
}
let some = numberForString(someString)
let other = numberForString(otherString)
Like this, the function would be executed with "Test" and "Hello there" when it really gets called in the program, when the app starts for example. Definitely at runtime. However if you change your function to take a StaticString
func numberForString(string: StaticString) -> Int {
return string.stringValue.unicodeScalars.reduce(0) { $0 * 1 << 8 + Int($1.value) }
}
the compiler knows that the passed in StaticString is knowable at compile time, so guess what it does? It runs the function right at compile time (How awesome is that!). I once read an article about that, the author inspected the generated assembly and he actually found the already computed numbers.
As you can see this can be useful in some cases like the one mentioned, to not decrease runtime performance for stuff that can be done at compile time.
EDIT: Dániel Nagy and me had a conversation. The above example of mine doesn't work because the function stringValue of StaticString can't be known at compile time (because it returns a String). Here is a better example:
func countStatic(string: StaticString) -> Int {
return string.byteSize // Breakpoint here
}
func count(string: String) -> Int {
return string.characters.count // Breakpoint here
}
let staticString : StaticString = "static string"
let string : String = "string"
print(countStatic(staticString))
print(count(string))
In a release build only the second breakpoint gets triggered whereas if you change the first function to
func countStatic(string: StaticString) -> Int {
return string.stringValue.characters.count // Breakpoint here
}
both breakpoints get triggered.
Apparently there are some methods which can be done at compile time while other can't. I wonder how the compiler figures this out actually.

Which programming languages allow default values for method parameters?

I'm curious which languages allow you to do something like this:
method foo(String bar = "beh"){
}
If you call foo like this:
foo();
bar will be set to "beh", but if you call like this:
foo("baz");
bar will be set to "baz".
almost all Lisps
Ruby
Python
C++
C#
Visual Basic.NET
Tcl
Visual Basic
Ioke
Seph
Cobra
Nemerle
Mirah
Delphi
Groovy
PHP
Fancy
Scala
PHP:
function foo($var = "foo") {
print $var;
}
foo(); // outputs "foo"
foo("bar"); // outputs "bar"
Python:
def myFun(var = "foo"):
print var
Ruby:
def foo(var="foo")
print var
end
Groovy:
def foo(var="foo") {
print var
}
Ones I can think of
C# 4.0
C++
VB.Net (all versions)
VB6
F# (members only)
Powershell
IDL
Ruby
Racket provides this, as well as keyword arguments:
(define (f x [y 0]) (+ x y))
(f 1) ; => 1
(f 10 20) ; => 30
(define (g x #:y [y 0]) (- x y))
(g 1) ; => 1
(g 10 #:y 20) ; => -10
They're described in the documentation.
Delphi has allowed this since about version 5 - released in 1999
procedure foo(const bar: string = 'beh');
begin
...
end;
foo;
foo('baz');
Perl does with Method::Signatures.
Python:
def foo(bar = value):
# This function can be invoked as foo() or foo(something).
# In the former case, bar will have its default value.
pass
as of c# 4.0 you can now have default params. Finally!
Also C++, Ruby and VB
TCL provides this functionality
proc procName {{arg1 defaultValue} {arg2 anotherDefaultValue}} {
# proc body
}
D:
void foo(int x, int y = 3)
{
...
}
...
foo(4); // same as foo(4, 3);
Fantom:
class Person
{
Int yearsToRetirement(Int retire := 65) { return retire - age }
Int age
}
Java has a workaround.
You can have the foo method with no parameters that calls the foo method with parameters setting the default value, like this:
void foo() {
foo("beh");
}
void foo(String bar) {
this.bar = bar;
}
Add to the list Realbasic (indeed Realbasic has almost every nice feature of every language I can think of, including introspection and sandboxed scripting).
Python. (But watch out for mutables per http://effbot.org/zone/default-values.htm )
And plenty of languages, including C, allow variable numbers of parameters which effectively lets you do the same thing.
In many modern scripting languages, including PHP, JavaScript, and Perl, a better idiom for handling such things is to allow an associative array or object as a parameter, and then assign defaults if needed.
e.g.
function foo( options ){
if( options.something === undefined ){
options.something = some_default_value;
}
...
}
This eliminates the necessity of putting the defaultable values at the end of the list of parameters and remembering all the stuff you don't want to override.
As always -- use in moderation.
You can do it in PL/SQL.
I know Python allows this, while C,C++ don't.

Resources