Skip to content

Instantly share code, notes, and snippets.

@kozross
Created February 23, 2017 04:42
Show Gist options
  • Save kozross/4d4b97ebca8e65fc4bd80bba5c7345e0 to your computer and use it in GitHub Desktop.
Save kozross/4d4b97ebca8e65fc4bd80bba5c7345e0 to your computer and use it in GitHub Desktop.
# I have the following bit of code
using Format
type MinimaxNode
id :: Int
min_node :: Bool
val :: Int
children :: Vector{MinimaxNode}
end
function f(old, # what type is this?
ignored :: Int,
depth :: Int,
branch :: Range{Int},
vrange :: Range{Int},
mini :: Bool)
(new, u) = raw_minimax(old[0], depth - 1, branch, vrange, !mini)
push!(u, old[1])
return (new, old[1])
end
function raw_minimax(ids, # this too
depth :: Int,
branch :: Range{Int},
vrange :: Range{Int},
mini :: Bool)
if depth == 0
u = MinimaxNode(first(ids), mini, rand(vrange), MinimaxNode[])
return (drop(ids, 1), u)
end
id = first(ids)
(ran, children) = foldl((x, y) -> f(x, y, depth, branch, vrange, mini),
(drop(ids, 1), MinimaxNode[]),
[1:rand(branch)])
vals = map(x -> x.val, children)
val = mini ? minimum(vals) : maximum(vals)
return (ran, MinimaxNode(id, mini, val, children))
end
function mk_minimax(depth :: Int,
branch :: Range{Int},
vrange :: Range{Int},
mini :: Bool)
(ignore, ans) = raw_minimax(countfrom(), depth, branch, vrange, mini)
return ans
end
# Aside from the unknown types above being something of a mystery, when I try the following (after `include`ing):
#
# julia> x = mk_minimax(2, 2:3, -10:10, true)
#
# I get this:
# ERROR: MethodError: no method matching f(::Tuple{Base.Drop{Base.Count{Int64}},Array{MinimaxNode,1}}, ::UnitRange{Int64},
# ::Int64, ::UnitRange{Int64}, ::UnitRange{Int64}, ::Bool)
# Closest candidates are: f(::Any, ::Int64, ::Int64, ::Range{Int64}, ::Range{Int64}, ::Bool) at /home/koz/documents/uni/teaching/2017/ai/mk-minimax.jl:16
# in mapfoldl_impl(::Base.#identity, ::##1#3{Int64,UnitRange{Int64},UnitRange{Int64},Bool}, ::Tuple{Base.Drop{Base.Count{Int64}},Array{MinimaxNode,1}}, ::Array{UnitRange{Int64},1}, ::Int64) at ./reduce.jl:46
# in raw_minimax(::Base.Count{Int64}, ::Int64, ::UnitRange{Int64}, ::UnitRange{Int64}, ::Bool) at /home/koz/documents/uni/teaching/2017/ai/mk-minimax.jl:34
# in mk_minimax(::Int64, ::UnitRange{Int64}, ::UnitRange{Int64}, ::Bool) at /home/koz/documents/uni/teaching/2017/ai/mk-minimax.jl:46
# in eval_user_input(::Any, ::Base.REPL.REPLBackend) at ./REPL.jl:64 in macro expansion at ./REPL.jl:95 [inlined]
# in (::Base.REPL.##3#4{Base.REPL.REPLBackend})() at ./event.jl:68
#
# What on earth is going on?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment