funzy funz-fun - wirr & weired in the kopp - Part I
Einfach mal so ...
julia> a = [1//2 1//4 1//4;1//4 1//2 1//4;1//4 1//4 1//2]
3×3 Array{Rational{Int64},2}:
1//2 1//4 1//4
1//4 1//2 1//4
1//4 1//4 1//2
julia> f(x) = a[x,1] + a[x,2] + a[x,3]
f (generic function with 1 method)
julia> f(1)
1//1
julia> for i in 1:3
f(i)
end
julia> for i in 1:3
println(f(i))
end
1//1
1//1
1//1
julia> f(X) = a[x,1] - a[x,2]
f (generic function with 1 method)
julia> for i in 1:3
println(f(i))
end
ERROR: UndefVarError: x not defined
Stacktrace:
[1] f(::Int64) at ./REPL[6]:1
[2] top-level scope at ./REPL[7]:2
julia> f(x) = a[x,1] - a[x,2]
f (generic function with 1 method)
julia> for i in 1:3
println(f(i))
end
1//4
-1//4
0//1
julia> f(x) = 1 - a[x,1] - a[x,2]
f (generic function with 1 method)
julia> for i in 1:3
println(f(i))
end
1//4
1//4
1//2
julia> a
3×3 Array{Rational{Int64},2}:
1//2 1//4 1//4
1//4 1//2 1//4
1//4 1//4 1//2
julia> function f(a1, opta2=2; key="foo")
println("normal argument: $a1")
println("optional argument: $opta2")
println("keyword argument: $key")
end
f (generic function with 2 methods)
julia> f(1,key=fals)
ERROR: UndefVarError: fals not defined
Stacktrace:
[1] top-level scope at REPL[14]:1
julia> f(1,key=3)
normal argument: 1
optional argument: 2
keyword argument: 3
julia> f(1,key=11)
normal argument: 1
optional argument: 2
keyword argument: 11
julia> f(1,key=11,44)
normal argument: 1
optional argument: 44
keyword argument: 11
julia> f(22,key=11,44)
normal argument: 22
optional argument: 44
keyword argument: 11
julia> f(22,33,key=11,44)
ERROR: MethodError: no method matching f(::Int64, ::Int64, ::Int64; key=11)
Closest candidates are:
f(::Any, ::Any; key) at REPL[13]:2
f(::Any) at REPL[13]:2 got unsupported keyword argument "key"
Stacktrace:
[1] top-level scope at REPL[19]:1
julia> f(22,33,key=11)
normal argument: 22
optional argument: 33
keyword argument: 11
julia> function f(a1, opta2=2; ki="foo")
println("normal argument: $a1")
println("optional argument: $opta2")
println("keyword argument: $ki")
end
f (generic function with 2 methods)
julia> f(22,33,key=11)
ERROR: MethodError: no method matching f(::Int64, ::Int64; key=11)
Closest candidates are:
f(::Any, ::Any; ki) at REPL[21]:2 got unsupported keyword argument "key"
f(::Any) at REPL[21]:2 got unsupported keyword argument "key"
Stacktrace:
[1] kwerr(::NamedTuple{(:key,),Tuple{Int64}}, ::Function, ::Int64, ::Int64) at ./error.jl:125
[2] (::getfield(Main, Symbol("#kw##f")))(::NamedTuple{(:key,),Tuple{Int64}}, ::typeof(f), ::Int64, ::Int64) at ./none:0
[3] top-level scope at REPL[22]:1
julia> function f(a3, opta2=2; key="foo")
println("normal argument: $a1")
println("optional argument: $opta2")
println("keyword argument: $key")
end
f (generic function with 2 methods)
julia> f(22,33,key=11)
ERROR: UndefVarError: a1 not defined
Stacktrace:
[1] #f#5(::Int64, ::typeof(f), ::Int64, ::Int64) at ./REPL[23]:2
[2] (::getfield(Main, Symbol("#kw##f")))(::NamedTuple{(:key,),Tuple{Int64}}, ::typeof(f), ::Int64, ::Int64) at ./none:0
[3] top-level scope at REPL[24]:1
julia> g(x, y) = sqrt(x^2 + y^2)
g (generic function with 1 method)
julia> g(11,22)
24.596747752497688
julia> function g(x,y)
xx = x*x
yy = sqrt(y)
return (yy,xx)
end
g (generic function with 1 method)
julia> g(22,33)
(5.744562646538029, 484)
julia> function paybills(bankbalance)
if bankbalance < 0
return false
else
return true
end
end
paybills (generic function with 1 method)
julia> paybills(111)
true
julia> paybills(-111)
false
julia> xyzpos(9.5.4)
ERROR: syntax: invalid numeric constant "9.5."
Stacktrace:
[1] top-level scope at REPL[32]:0
julia> xyzpos(9,5,4)
9, 5, 4
julia> xyzpos(9,5)
9, 5, 0
julia> function f(p, q ; r = 4, s = "hello")
println("p is $p")
println("q is $q")
return "r => $r, s => $s"
end
f (generic function with 2 methods)
julia> f(1,2)
p is 1
q is 2
"r => 4, s => hello"
julia> f("a", "b", r=pi, s=22//7)
p is a
q is b
"r => π, s => 22//7"
julia> f(r=999, 1, 2)
p is 1
q is 2
"r => 999, s => hello"
julia> f(s="hello world", r=999, 1, 2)
p is 1
q is 2
"r => 999, s => hello world"
julia> function fvar(args...)
println("you supplied $(length(args)) arguments")
for arg in args
println(" argument ", arg)
end
end
fvar (generic function with 1 method)
julia> fvar("hallo",22,3)
you supplied 3 arguments
argument hallo
argument 22
argument 3
julia> fvar(3,4,5,6,7,8,9,6,54,3,)
you supplied 10 arguments
argument 3
argument 4
argument 5
argument 6
argument 7
argument 8
argument 9
argument 6
argument 54
argument 3
julia> function fill_with_5(x)
x[:] .= 5
end
fill_with_5 (generic function with 1 method)
julia> x = collect(1:10);
julia> fill_with_5(x)
10-element view(::Array{Int64,1}, :) with eltype Int64:
5
5
5
5
5
5
5
5
5
5
julia> x
10-element Array{Int64,1}:
5
5
5
5
5
5
5
5
5
5
julia> function fill_with_5(x)
x[:] .= 55
end
fill_with_5 (generic function with 1 method)
julia> fill_with_5(x);
julia> x
10-element Array{Int64,1}:
55
55
55
55
55
55
55
55
55
55
julia> map(x -> x^2 + 2x - 1, [1,3,-1])
3-element Array{Int64,1}:
2
14
-2
julia> x
10-element Array{Int64,1}:
55
55
55
55
55
55
55
55
55
55
;-)
julia> map(x -> println(x),["Huhu!","Hallo!","Welt, du böse, du!"])
Huhu!
Hallo!
Welt, du böse, du!
3-element Array{Nothing,1}:
nothing
nothing
nothing
map((x,y,z) -> x + y + z, [1,2,3], [4, 5, 6], [7, 8, 9])
3-element Array{Int64,1}:
12
15
18
julia> random = () -> rand(0:10)
#15 (generic function with 1 method)
julia> random()
0
julia> random()
0
julia> random()
4
julia> a=1:111;
julia> map(sin,a)
111-element Array{Float64,1}:
0.8414709848078965
0.9092974268256817
0.1411200080598672
-0.7568024953079282
-0.9589242746631385
-0.27941549819892586
0.6569865987187891
0.9893582466233818
0.4121184852417566
-0.5440211108893698
⋮
0.6229886314423488
-0.32162240316253093
-0.9705352835374847
-0.7271425000808526
0.18478174456066745
0.926818505417785
0.8167426066363169
-0.044242678085070965
-0.8645514486106083
julia> @time map(sin, 1:111111);
0.007733 seconds (10 allocations: 868.422 KiB)
julia> @time sin.(1:111111);
0.164148 seconds (222.40 k allocations: 12.247 MiB)
julia> map(println, 1:3)
1
2
3
3-element Array{Nothing,1}:
nothing
nothing
nothing
julia> foreach(println,1:3)
1
2
3
julia> map(*, 5:24, fill(32,20))
20-element Array{Int64,1}:
160
192
224
256
288
320
352
384
416
448
480
512
544
576
608
640
672
704
736
768
julia> map(//, 5:24, fill(32,20))
20-element Array{Rational{Int64},1}:
5//32
3//16
7//32
1//4
9//32
5//16
11//32
3//8
13//32
7//16
15//32
1//2
17//32
9//16
19//32
5//8
21//32
11//16
23//32
3//4
julia> [5:24 fill(32,20)]
20×2 Array{Int64,2}:
5 32
6 32
7 32
8 32
9 32
10 32
11 32
12 32
⋮
17 32
18 32
19 32
20 32
21 32
22 32
23 32
24 32
julia> reduce(+, 1:10)
55
julia> reduce(*, 1:10)
3628800
julia> reduce(/, 1:10)
2.7557319223985894e-7
julia> reduce(-, 1:10)
-53
julia> l(a, b) = (println("comparing \"$a\" and \"$b\""); length(a) > length(b) ? a : b)
l (generic function with 1 method)
julia> reduce(l, split("This is a sentence containing some very long strings"))
comparing "This" and "is"
comparing "This" and "a"
comparing "This" and "sentence"
comparing "sentence" and "containing"
comparing "containing" and "some"
comparing "containing" and "very"
comparing "containing" and "long"
comparing "containing" and "strings"
"containing"
Izz goin glaisch weyda. Wörd!
* Immer wieder an unseren Großweisen Roberto Blanco denken: Ein bissi Spaß muss sein!
Learnin' by foolin' or fuelin' or some- or anythin'*
julia> a = [1//2 1//4 1//4;1//4 1//2 1//4;1//4 1//4 1//2]
3×3 Array{Rational{Int64},2}:
1//2 1//4 1//4
1//4 1//2 1//4
1//4 1//4 1//2
julia> f(x) = a[x,1] + a[x,2] + a[x,3]
f (generic function with 1 method)
julia> f(1)
1//1
julia> for i in 1:3
f(i)
end
julia> for i in 1:3
println(f(i))
end
1//1
1//1
1//1
julia> f(X) = a[x,1] - a[x,2]
f (generic function with 1 method)
julia> for i in 1:3
println(f(i))
end
ERROR: UndefVarError: x not defined
Stacktrace:
[1] f(::Int64) at ./REPL[6]:1
[2] top-level scope at ./REPL[7]:2
julia> f(x) = a[x,1] - a[x,2]
f (generic function with 1 method)
julia> for i in 1:3
println(f(i))
end
1//4
-1//4
0//1
julia> f(x) = 1 - a[x,1] - a[x,2]
f (generic function with 1 method)
julia> for i in 1:3
println(f(i))
end
1//4
1//4
1//2
julia> a
3×3 Array{Rational{Int64},2}:
1//2 1//4 1//4
1//4 1//2 1//4
1//4 1//4 1//2
julia> function f(a1, opta2=2; key="foo")
println("normal argument: $a1")
println("optional argument: $opta2")
println("keyword argument: $key")
end
f (generic function with 2 methods)
julia> f(1,key=fals)
ERROR: UndefVarError: fals not defined
Stacktrace:
[1] top-level scope at REPL[14]:1
julia> f(1,key=3)
normal argument: 1
optional argument: 2
keyword argument: 3
julia> f(1,key=11)
normal argument: 1
optional argument: 2
keyword argument: 11
julia> f(1,key=11,44)
normal argument: 1
optional argument: 44
keyword argument: 11
julia> f(22,key=11,44)
normal argument: 22
optional argument: 44
keyword argument: 11
julia> f(22,33,key=11,44)
ERROR: MethodError: no method matching f(::Int64, ::Int64, ::Int64; key=11)
Closest candidates are:
f(::Any, ::Any; key) at REPL[13]:2
f(::Any) at REPL[13]:2 got unsupported keyword argument "key"
Stacktrace:
[1] top-level scope at REPL[19]:1
julia> f(22,33,key=11)
normal argument: 22
optional argument: 33
keyword argument: 11
julia> function f(a1, opta2=2; ki="foo")
println("normal argument: $a1")
println("optional argument: $opta2")
println("keyword argument: $ki")
end
f (generic function with 2 methods)
julia> f(22,33,key=11)
ERROR: MethodError: no method matching f(::Int64, ::Int64; key=11)
Closest candidates are:
f(::Any, ::Any; ki) at REPL[21]:2 got unsupported keyword argument "key"
f(::Any) at REPL[21]:2 got unsupported keyword argument "key"
Stacktrace:
[1] kwerr(::NamedTuple{(:key,),Tuple{Int64}}, ::Function, ::Int64, ::Int64) at ./error.jl:125
[2] (::getfield(Main, Symbol("#kw##f")))(::NamedTuple{(:key,),Tuple{Int64}}, ::typeof(f), ::Int64, ::Int64) at ./none:0
[3] top-level scope at REPL[22]:1
julia> function f(a3, opta2=2; key="foo")
println("normal argument: $a1")
println("optional argument: $opta2")
println("keyword argument: $key")
end
f (generic function with 2 methods)
julia> f(22,33,key=11)
ERROR: UndefVarError: a1 not defined
Stacktrace:
[1] #f#5(::Int64, ::typeof(f), ::Int64, ::Int64) at ./REPL[23]:2
[2] (::getfield(Main, Symbol("#kw##f")))(::NamedTuple{(:key,),Tuple{Int64}}, ::typeof(f), ::Int64, ::Int64) at ./none:0
[3] top-level scope at REPL[24]:1
julia> g(x, y) = sqrt(x^2 + y^2)
g (generic function with 1 method)
julia> g(11,22)
24.596747752497688
julia> function g(x,y)
xx = x*x
yy = sqrt(y)
return (yy,xx)
end
g (generic function with 1 method)
julia> g(22,33)
(5.744562646538029, 484)
julia> function paybills(bankbalance)
if bankbalance < 0
return false
else
return true
end
end
paybills (generic function with 1 method)
julia> paybills(111)
true
julia> paybills(-111)
false
julia> xyzpos(9.5.4)
ERROR: syntax: invalid numeric constant "9.5."
Stacktrace:
[1] top-level scope at REPL[32]:0
julia> xyzpos(9,5,4)
9, 5, 4
julia> xyzpos(9,5)
9, 5, 0
julia> function f(p, q ; r = 4, s = "hello")
println("p is $p")
println("q is $q")
return "r => $r, s => $s"
end
f (generic function with 2 methods)
julia> f(1,2)
p is 1
q is 2
"r => 4, s => hello"
julia> f("a", "b", r=pi, s=22//7)
p is a
q is b
"r => π, s => 22//7"
julia> f(r=999, 1, 2)
p is 1
q is 2
"r => 999, s => hello"
julia> f(s="hello world", r=999, 1, 2)
p is 1
q is 2
"r => 999, s => hello world"
julia> function fvar(args...)
println("you supplied $(length(args)) arguments")
for arg in args
println(" argument ", arg)
end
end
fvar (generic function with 1 method)
julia> fvar("hallo",22,3)
you supplied 3 arguments
argument hallo
argument 22
argument 3
julia> fvar(3,4,5,6,7,8,9,6,54,3,)
you supplied 10 arguments
argument 3
argument 4
argument 5
argument 6
argument 7
argument 8
argument 9
argument 6
argument 54
argument 3
julia> function fill_with_5(x)
x[:] .= 5
end
fill_with_5 (generic function with 1 method)
julia> x = collect(1:10);
julia> fill_with_5(x)
10-element view(::Array{Int64,1}, :) with eltype Int64:
5
5
5
5
5
5
5
5
5
5
julia> x
10-element Array{Int64,1}:
5
5
5
5
5
5
5
5
5
5
julia> function fill_with_5(x)
x[:] .= 55
end
fill_with_5 (generic function with 1 method)
julia> fill_with_5(x);
julia> x
10-element Array{Int64,1}:
55
55
55
55
55
55
55
55
55
55
julia> map(x -> x^2 + 2x - 1, [1,3,-1])
3-element Array{Int64,1}:
2
14
-2
julia> x
10-element Array{Int64,1}:
55
55
55
55
55
55
55
55
55
55
;-)
julia> map(x -> println(x),["Huhu!","Hallo!","Welt, du böse, du!"])
Huhu!
Hallo!
Welt, du böse, du!
3-element Array{Nothing,1}:
nothing
nothing
nothing
map((x,y,z) -> x + y + z, [1,2,3], [4, 5, 6], [7, 8, 9])
3-element Array{Int64,1}:
12
15
18
julia> random = () -> rand(0:10)
#15 (generic function with 1 method)
julia> random()
0
julia> random()
0
julia> random()
4
julia> a=1:111;
julia> map(sin,a)
111-element Array{Float64,1}:
0.8414709848078965
0.9092974268256817
0.1411200080598672
-0.7568024953079282
-0.9589242746631385
-0.27941549819892586
0.6569865987187891
0.9893582466233818
0.4121184852417566
-0.5440211108893698
⋮
0.6229886314423488
-0.32162240316253093
-0.9705352835374847
-0.7271425000808526
0.18478174456066745
0.926818505417785
0.8167426066363169
-0.044242678085070965
-0.8645514486106083
julia> @time map(sin, 1:111111);
0.007733 seconds (10 allocations: 868.422 KiB)
julia> @time sin.(1:111111);
0.164148 seconds (222.40 k allocations: 12.247 MiB)
julia> map(println, 1:3)
1
2
3
3-element Array{Nothing,1}:
nothing
nothing
nothing
julia> foreach(println,1:3)
1
2
3
julia> map(*, 5:24, fill(32,20))
20-element Array{Int64,1}:
160
192
224
256
288
320
352
384
416
448
480
512
544
576
608
640
672
704
736
768
julia> map(//, 5:24, fill(32,20))
20-element Array{Rational{Int64},1}:
5//32
3//16
7//32
1//4
9//32
5//16
11//32
3//8
13//32
7//16
15//32
1//2
17//32
9//16
19//32
5//8
21//32
11//16
23//32
3//4
julia> [5:24 fill(32,20)]
20×2 Array{Int64,2}:
5 32
6 32
7 32
8 32
9 32
10 32
11 32
12 32
⋮
17 32
18 32
19 32
20 32
21 32
22 32
23 32
24 32
julia> reduce(+, 1:10)
55
julia> reduce(*, 1:10)
3628800
julia> reduce(/, 1:10)
2.7557319223985894e-7
julia> reduce(-, 1:10)
-53
julia> l(a, b) = (println("comparing \"$a\" and \"$b\""); length(a) > length(b) ? a : b)
l (generic function with 1 method)
julia> reduce(l, split("This is a sentence containing some very long strings"))
comparing "This" and "is"
comparing "This" and "a"
comparing "This" and "sentence"
comparing "sentence" and "containing"
comparing "containing" and "some"
comparing "containing" and "very"
comparing "containing" and "long"
comparing "containing" and "strings"
"containing"
Izz goin glaisch weyda. Wörd!
* Immer wieder an unseren Großweisen Roberto Blanco denken: Ein bissi Spaß muss sein!
Kommentare
Kommentar veröffentlichen