minimumby function for Julia? - julia

Is there a minimumby function for Julia (or some idiomatic substitute)? That is, I want something that works like:
julia> minimumby(length, ["Julion", "Julia", "Jule"])
"Jule"

I came up with the following, which seems to do what I want. Not sure how optimal it is.
function minimumby(keyf, a)
keymap = item -> (keyf(item), item)
keymin = (x, y) -> if y[1] < x[1] y else x end
mapreduce(keymap, keymin, a)[2]
end

No, unfortunately not. This had been requested for years, but no-one got around to implementing it.

Best I can do is:
julia> reduce((a, b) -> length(a) < length(b) ? a : b, ["Julion", "Julia", "Jule"])
"Jule"

Related

[julia]syntax: invalid assignment location "getproperty(f, n)"

My julia version is 1.7.1.
These code can recurrent my problem:
struct Foo1
sixsix
aa
bb
disposion
end
struct Foo2
aa
bb
end
function Base.:+(f1::Foo1, f2 :: Foo2)
newf = f1
for n in fieldnames(typeof(f2))
getproperty(newf, n) += getproperty(f2, n)
end
return newf
end
returned LoadError: syntax: invalid assignment location "getproperty(newf, n)"
same LoadError happened when I try to use getfield:
function Base.:+(f1::Foo1, f2 :: Foo2)
newf = f1
for n in fieldnames(typeof(f2))
getfield(newf, n) += getfield(f2, n)
end
return newf
end
Firstly, you must make your structs mutable for this to work.
Secondly, this:
getproperty(newf, n) += getproperty(f2, n)
is expanded into
getproperty(newf, n) = getproperty(newf, n) + getproperty(f2, n)
In other words you are apparently trying to assign a value into a function call. In Julia you can only assign to variables, not to values. The only thing this syntax is allowed for is function definition. From the manual:
There is a second, more terse syntax for defining a function in Julia.
The traditional function declaration syntax demonstrated above is
equivalent to the following compact "assignment form":
julia> f(x,y) = x + y
f (generic function with 1 method)
So the syntax you are using doesn't do what you want, and what you want to do is not possible anyway, since you can only assign to variables, not to values.
What you are trying to do can be achieved like this (assuming n is a Symbol):
setfield!(newf, n, getfield(newf, n) + getfield(f2, n))
I would rather recommend you to do the following:
Base.:+(f1::Foo1, f2 :: Foo2) =
Foo1(f1.sixsix, f1.aa+f2.aa, f1.bb+f2.bb, f1.disposion)
Your code is wrong for the following reasons:
Foo1 is not mutable, so you cannot change values of its elements
to set a field of mutable struct (which your struct is not) you use setfield!
You mix properties and fields of a struct which are not the same.
fieldnames works on types not on instances of type.
If you wanted to be more fancy and do automatic detection of matching fields you could do:
Base.:+(f1::Foo1, f2 :: Foo2) =
Foo1((n in fieldnames(Foo2) ?
getfield(f1, n) + getfield(f2, n) :
getfield(f1, n) for n in fieldnames(Foo1))...)
However, I would not recommend it, as I feel that it is overly complicated.

Erlang, why is my pattern matching not working for equations inside equations?

I have an evaluation function (expr_eval/1) which should evaluate an
expression and return a
number. As of this moment my function is returning valid numbers for equations such as:
expr_eval({plus,{num, 17},{num,10}}) → 27
but not for this:
expr_eval({mul,{plus,{num,5},{num,8}},{num,3}}) → 39
My Function:
expr_eval({mul, {num,A}, {num,B}}) ->A*B;
expr_eval({plus, {num,A}, {num,B}}) ->A+B;
expr_eval({minus, {num,A},{num,B}}) ->A-B;
expr_eval({_,_,_})->nothing_matched.
I understand that it is only taking the patterns as it is, but how can I fix this problem? I could hard code it and add the below code to my function and the above equation works fine, but it wouldn't apply to other equations.(e.g. expr_eval({minus,{plus,{num,5},{num,8}},{num,3}}) → 8)
expr_eval({mul, {plus,{num,A},{num,B}},{num,C}}) ->(A+B)*C;
well I believe it must be recursive function, something like
expr({num,X}) -> X;
expr({plus, X, Y}) -> expr(X) + expr(Y);
expr({minus, X, Y}) -> expr(X) - expr(Y);
expr({mul,X, Y}) -> expr(X)*expr(Y);
expr(Any) -> io:format("Dont know what to do with ~p~n",[Any]).

Getting count of occurrences for X in string

Im looking for a function like Pythons
"foobar, bar, foo".count("foo")
Could not find any functions that seemed able to do this, in a obvious way. Looking for a single function or something that is not completely overkill.
Julia-1.0 update:
For single-character count within a string (in general, any single-item count within an iterable), one can use Julia's count function:
julia> count(i->(i=='f'), "foobar, bar, foo")
2
(The first argument is a predicate that returns a ::Bool).
For the given example, the following one-liner should do:
julia> length(collect(eachmatch(r"foo", "bar foo baz foo")))
2
Julia-1.7 update:
Starting with Julia-1.7 Base.Fix2 can be used, through ==('f') below, as to shorten and sweeten the syntax:
julia> count(==('f'), "foobar, bar, foo")
2
What about regexp ?
julia> length(matchall(r"ba", "foobar, bar, foo"))
2
I think that right now the closest built-in thing to what you're after is the length of a split (minus 1). But it's not difficult to specifically create what you're after.
I could see a searchall being generally useful in Julia's Base, similar to matchall. If you don't care about the actual indices, you could just use a counter instead of growing the idxs array.
function searchall(s, t; overlap::Bool=false)
idxfcn = overlap ? first : last
r = findnext(s, t, firstindex(t))
idxs = typeof(r)[] # Or to only count: n = 0
while r !== nothing
push!(idxs, r) # n += 1
r = findnext(s, t, idxfcn(r) + 1)
end
idxs # return n
end
Adding an answer to this which allows for interpolation:
julia> a = ", , ,";
julia> b = ",";
julia> length(collect(eachmatch(Regex(b), a)))
3
Actually, this solution breaks for some simple cases due to use of Regex. Instead one might find this useful:
"""
count_flags(s::String, flag::String)
counts the number of flags `flag` in string `s`.
"""
function count_flags(s::String, flag::String)
counter = 0
for i in 1:length(s)
if occursin(flag, s)
s = replace(s, flag=> "", count=1)
counter+=1
else
break
end
end
return counter
end
Sorry to post another answer instead of commenting previous one, but i've not managed how to deal with code blocks in comments :)
If you don't like regexps, maybe a tail recursive function like this one (using the search() base function as Matt suggests) :
function mycount(what::String, where::String)
function mycountacc(what::String, where::String, acc::Int)
res = search(where, what)
res == 0:-1 ? acc : mycountacc(what, where[last(res) + 1:end], acc + 1)
end
what == "" ? 0 : mycountacc(what, where, 0)
end
This is simple and fast (and does not overflow the stack):
function mycount2(where::String, what::String)
numfinds = 0
starting = 1
while true
location = search(where, what, starting)
isempty(location) && return numfinds
numfinds += 1
starting = location.stop + 1
end
end
one liner: (Julia 1.3.1):
julia> sum([1 for i = eachmatch(r"foo", "foobar, bar, foo")])
2
Since Julia 1.3, there has been a count method that does exactly this.
count(
pattern::Union{AbstractChar,AbstractString,AbstractPattern},
string::AbstractString;
overlap::Bool = false,
)
Return the number of matches for pattern in string.
This is equivalent to calling length(findall(pattern, string)) but more
efficient.
If overlap=true, the matching sequences are allowed to overlap indices in the
original string, otherwise they must be from disjoint character ranges.
│ Julia 1.3
│
│ This method requires at least Julia 1.3.
julia> count("foo", "foobar, bar, foo")
2
julia> count("ana", "bananarama")
1
julia> count("ana", "bananarama", overlap=true)
2

Miranda while- and for-loops

I'm looking for a way to do while-loops or for-loops in Miranda.
I'm trying to do something like
while(blablanotfinished)
{
if(a=true)blabla
else blabla
}
Miranda doesn't have while- or for-loops (which wouldn't make much sense without mutable state anyway). In most cases you can use higher order functions instead. In cases where there is no higher-order function which does what you need, you can use recursion.
For example if you have the following while-loop in an imperative language:
f(start) {
x = start
while( !finished(x) ) {
x = next(x)
}
return x
}
You would express it recursively in Miranda like this:
f x = if finished x then x else f (next x)
In Miranda (and in general, in purely functional programming languages) the use of looping constructs like WHILE, FOR, etc. is discouraged. You're expected to do iteration via recursion.
Like many other functional languages, Miranda does not have for- or while-loops. Instead, you write loops using recursion, list comprehensions or higher-order functions.
while/repeat/for-loops in functional programming style look like this:
while :: (*->bool) -> (*->*) -> * -> *
while p ff state
= state , if ~ (p state)
= while p ff (ff state), otherwise
Sample: Add number to its reverse until it is a palindrome.
Hints: Start value 196 leads to VERY big numbers.
isPalindrome :: num -> bool
isPalindrome n = (str = reverse str) where str = shownum n
addPalindrome :: num -> num
addPalindrome n = n + rev where rev = (numval.reverse.shownum) n
test196 :: num -> num
test196 n = while ((~).isPalindrome) addPalindrome n
test = test196 89
I hope, somebody is still interested in Gofer/Miranda/Haskell.
Annemarie Paysen (Germany)

map function if a predicate holds

I feel like this should be fairly obvious, or easy, but I just can't get it. What I want to do is apply a function to a list (using map) but only if a condition is held. Imagine you only wanted to divide the numbers which were even:
map (`div` 2) (even) [1,2,3,4]
And that would give out [1,1,3,2] since only the even numbers would have the function applied to them. Obviously this doesn't work, but is there a way to make this work without having to write a separate function that you can give to map? filter is almost there, except I also want to keep the elements which the condition doesn't hold for, and just not apply the function to them.
If you don't want to define separate function, then use lambda.
map (\x -> if (even x) then (x `div` 2) else x) [1,2,3,4]
Or instead of a map, list comprehension, bit more readable I think.
[if (even x) then (x `div` 2) else x | x <- [1,2,3,4]]
mapIf p f = map (\x -> if p x then f x else x)
In addition to the answer of PiotrLegnica: Often, it's easier to read if you declare a helper function instead of using a lambda. Consider this:
map helper [1..4] where
helper x | even x = x `div` 2
| otherwise = x
([1..4] is sugar for [1,2,3,4])
If you want to remove all the other elements instead, consider using filter. filter removes all elements that don't satisfy the predicate:
filter even [1..4] -> [2,4]
So you can build a pipe of mapand filter than or use list-comprehension instead:
map (`div` 2) $ filter even [1..4]
[x `div` 2 | x <- [1..4], even x]
Choose whatever you like best.
Make your own helper function maker:
ifP pred f x =
if pred x then f x
else x
custom_f = ifP even f
map custom_f [..]
(caveat: I don't have access to a compiler right now. I guess this works OK...)
I like the other, more general solutions, but in your very special case you can get away with
map (\x -> x `div` (2 - x `mod` 2)) [1..4]
Mostly a rip off of existing answers, but according to my biased definition of "readable" (I like guards more than ifs, and where more than let):
mapIf p f = map f'
where f' x | p x = f x | otherwise = x
ghci says it probably works
ghci> let mapIf p f = map f' where f' x | p x = f x | otherwise = x
ghci> mapIf even (+1) [1..10]
[1,3,3,5,5,7,7,9,9,11]

Resources