julia – Ive Moved http://www.lindonslog.com Thu, 03 Nov 2016 17:07:09 +0000 en-US hourly 1 https://wordpress.org/?v=4.6.1 http://www.lindonslog.com/wp-content/uploads/2015/09/cropped-L-32x32.png julia – Ive Moved http://www.lindonslog.com 32 32 Partial Application for Functions in Julia http://www.lindonslog.com/programming/partial-application-functions-julia/ http://www.lindonslog.com/programming/partial-application-functions-julia/#respond Thu, 03 Nov 2016 17:07:09 +0000 http://www.lindonslog.com/?p=1181 Julia is purportedly a multi-paradigm language but I find their support for functional paradigms to be lacking. One feature that I looked for was Currying or Partial Application which corresponds to converting a function of multiple arguments into a sequence of single argument functions and taking a multiple argument function and fixing some of the […]

The post Partial Application for Functions in Julia appeared first on Ive Moved.

]]>
Julia is purportedly a multi-paradigm language but I find their support for functional paradigms to be lacking. One feature that I looked for was Currying or Partial Application which corresponds to converting a function of multiple arguments into a sequence of single argument functions and taking a multiple argument function and fixing some of the arguments to produce a new function of lesser arity respectively. In Clojure one has the partial function for the latter. Here is my attempt to emulate this behaviour in Julia.

Partial Application

function partial(f,a...)
        ( (b...) -> f(a...,b...) )
end

The function returns a lambda where the a… parameters are fixed. If you don’t know what the ellipsis do, check the documentation for “splat”. One can check the behavour is as expected

julia> function testf(x,y,z)
         2*x+y*y+x*y*z
       end
testf (generic function with 1 method)
julia> testf(2,3,4)
37
julia> partial(testf,2)(3,4)
37
julia> partial(testf,2,3)(4)
37

Of course you could just use a lambda.

The post Partial Application for Functions in Julia appeared first on Ive Moved.

]]>
http://www.lindonslog.com/programming/partial-application-functions-julia/feed/ 0
Passing Julia Type to C Function as Struct http://www.lindonslog.com/programming/passing-julia-type-to-c-function-as-struct/ http://www.lindonslog.com/programming/passing-julia-type-to-c-function-as-struct/#respond Wed, 24 Feb 2016 18:28:06 +0000 http://www.lindonslog.com/?p=1146 There seems to be hardly any examples to be found about calling C from Julia (perhaps because people feel no need to do so). Moreover the docs are, at least to me, quite cryptic. So if anyone wants to get started here is a minimal example. Julia Code: C Code: Compilation: gcc -fPIC -shared rosetta.c […]

The post Passing Julia Type to C Function as Struct appeared first on Ive Moved.

]]>
There seems to be hardly any examples to be found about calling C from Julia (perhaps because people feel no need to do so). Moreover the docs are, at least to me, quite cryptic. So if anyone wants to get started here is a minimal example.

Julia Code:

type mystruct                                                                                                                                                                                                      
        n::Int32                                                                                                                                                                                                   
end 

C Code:

struct mystruct{
	int n;
};
void structure(struct mystruct * input);
void sructure(struct mystruct * input){
	(*input).n=99;
}

Compilation:
gcc -fPIC -shared rosetta.c -o rosetta.so

Execution:
julia> test=mystruct(10)
mystruct(10)

julia> ccall((:structure, "rosetta.so"), Void, (Ref{mystruct},), test);

julia> test
mystruct(99)

The post Passing Julia Type to C Function as Struct appeared first on Ive Moved.

]]>
http://www.lindonslog.com/programming/passing-julia-type-to-c-function-as-struct/feed/ 0