recentpopularlog in

kme : fp   36

"The Mess We're In" by Joe Armstrong - YouTube
"...and now for the tricky bit."
I find that I do write really big comments - when I am thinking through the problem. Then they quickly become irrelevant, or even misleading, as you refine the approach, refactor, and generally do things differently. To keep the comments in alignment with the code doubles your effort.
erlang  fp  video  talk  joearmstrong  forthecomments 
october 2019 by kme
transform hash key from string into symbol - Ruby Forum | https://www.ruby-forum.com/
This was handy, but I found a different way by just iterating over the array and returning another (two-element) array inside a 'map' or a 'each_key' or something, then passing this to the Hash#[] constructor.

<code class="language-ruby">class Hash
def symbolize_keys
replace(inject({}) { |h,(k,v)| h[k.to_sym] = v; h })
end
end</code>
ruby  arrays  hashes  map  fp  idiom  newbie  syntax  solution 
may 2018 by kme
ruby - Difference Between map and each - Stack Overflow | https://stackoverflow.com/

each simply iterates over the given enumerable, running the block for each value. It discards the return value of the block, and each simply returns the original object it was called on:
<code class="language-ruby">[1, 2, 3].each do |i|
i + 1
end # => [1, 2, 3]
</code>

This is simply a nicer, more universal way of doing a traditional iterating for loop, and each is much preferred over for loops in Ruby (in fact, I don't think I've ever used a for loop in Ruby).

map, however, iterates over each element, using the return value of the block to populate a new array at each respective index and return that new array:
<code class="language-ruby">[1, 2, 3].map do |i|
i + 1
end # => [2, 3, 4]</code>

So it "maps" each element to a new one using the block given, hence the name "map". Note that neither each nor map themselves modify the original collection.
ruby  arrays  fp  map  syntax  newbie  reference  solution 
may 2018 by kme
Functional Programming – OCaml
So far so simple. If you're familiar with C/C++ then this looks like passing a function pointer around. Java has some sort of abomination called an anonymous class which is like a dumbed-down, slow and long-winded closure. If you know Perl then you probably already know and use Perl's closures and Perl's map function, which is exactly what we're talking about. The fact is that Perl is actually quite a good functional language.
perl  ocaml  functional  programming  fp  howto 
april 2016 by kme
Pass-by-reference the third parameter in PHP array_walk, without a warning - Stack Overflow
$myArray = array();
$callback = function ($key, $value) use (&$myArray) {
if (strlen($value) <= 2) {
$myArray[] = $key . $value;
}
};
array_walk($a, $callback);
php  arrays  fp  solution 
november 2015 by kme
call-with-current-continuation [http://community.schemewiki.org/]

(define (cor1 cont)
(let loop ()
(begin (display "in cor1\n")
(set! cont (call/cc cont)))
(loop)))

(define (cor2 cont)
(let loop ()
(begin (display "in cor2\n")
(set! cont (call/cc cont)))
(loop)))

callcc  continuations  scheme  fp 
march 2015 by kme

Copy this bookmark:





to read