Closed bethandtownes closed 4 years ago
fminbnd_if
does below, right? For given
fminbnd_if
computes min { f(x) | x ∈ X and p(x) } in O(N) if it exists.
If my understanding is correct, this fminbnd_if
itself is too specific and I'm not so interested. Of course it seems useful for the LeetCode problem, but unusable for other problems.
However, the style of functional programming is good one :+1:
This style is useful, and valuable to know even if you don't use the style.
By the way, in Haskell, a purely functional programming language, your fminbnd_if
can be written just as minimum $ map f $ filter p [from, from + step, to - 1]
, or like below. If you are interested in functional programming, I recommend try to learn Haskell :smile:
fminbnd_if :: (Num x, Ord y) => (x -> y) -> (x -> Bool) -> y
fminbnd_if f p fallback (from, to, step) =
let ys = map f $ filter p [from, from + step, to - 1]
in if ys == [] then fallback else minimum ys
wow. This is really concise code. Too bad that leetcode does not support haskell:( I think in c++20 with ranges c++ will gain similar expressive power coupled with lazy eval. But so far with c++17 standard it might not be possible:((
Hi kymk
I learnt a bit of functional programming and came up with a few handy functions. And I would like to share them with you. The `fminbnd_if' function below is useful in solving a handful of DP problems. The example problem is from Leetcode 1000 (https://leetcode.com/problems/minimum-cost-to-merge-stones/description/)