juan_gandhi: (Default)
[personal profile] juan_gandhi
Just discovered this trick that is pretty ubiquitous in JavaScript (and, probably in Scheme too).

((n:Int) => if (n %2 == 1) n*3 + 1 else n/2)((2 to 1000) filter isAmicable max)


The trick consists of defining a function and applying it right away to a value;

Date: 2011-10-11 06:55 pm (UTC)
From: [identity profile] antilamer.livejournal.com
This is what happens when you don't have "let" or "where".
(and this is what they can be desugared to)

Date: 2011-10-11 07:17 pm (UTC)
From: [identity profile] shabunc.livejournal.com
"let" or "where" can be desugared to anonymous function call only if there exists any kind of scope besides the global one.

I wouldn't call it desugaring.

Date: 2011-10-11 10:34 pm (UTC)
From: [identity profile] ivan-gandhi.livejournal.com
It's closer to CPT, no?

Date: 2011-10-11 10:59 pm (UTC)
From: [identity profile] jakobz.livejournal.com
В javascript это юзается чтобы получить новую область видимости для локальных переменных. В языках где все нормально сделано, не видел смысла в этом трюке. Код что-то не понял, что значит ((2 to 1000) isAmicable max) ?

Date: 2011-10-12 12:48 am (UTC)
From: [identity profile] ivan-gandhi.livejournal.com
А, слово пропустил (пофиксил)
isAmicable - из Euler 21.

Date: 2011-10-12 12:49 am (UTC)
From: [identity profile] ivan-gandhi.livejournal.com
смысл в трюке пока и самому не понятен. :)

Date: 2011-10-12 03:42 am (UTC)
From: [identity profile] dmitry-vk.livejournal.com
А еще из-за того, что операторы передачи управления (if, for, while и пр.) не возвращают результат последнего вычисленного выражения.

Date: 2011-10-12 04:59 am (UTC)
From: [identity profile] ivan-gandhi.livejournal.com
Это всё монады; они к себе вбирают, обратно не выпускают. Для выпуска нужна комонада.

Profile

juan_gandhi: (Default)
Juan-Carlos Gandhi

July 2025

S M T W T F S
  12345
6789 1011 12
131415 1617 1819
20212223242526
2728 293031  

Most Popular Tags

Style Credit

Expand Cut Tags

No cut tags
Page generated Jul. 31st, 2025 07:27 pm
Powered by Dreamwidth Studios