Lambda World Meetup 'Q: Programming as a Tool of Thought' – Jesús López-González

preview_player
Показать описание
Shocking as it might sound, these one-liners in the Q language, "{(*/)1+til x}" and "{x{x,sum -2#x}/1 1}", implement the factorial and fibonacci functions, respectively. Yep. You might think that this terse syntax, commonly found in array programming languages, is the result of a quest for awkwardness and obfuscation, but nothing far from the truth: actually, it’s intended as a tool that will allow us to reason about our domain processes in a more effective way. This talk will unveil the design patterns underneath this syntax, and will relate them to familiar abstractions from functional programming (higher-order functions, functors, applicative programming, etc.). We will use Scala to exemplify equivalent implementations, and facilitate understanding. In sum, the takeaway of the talk is: do you like functional programming? Then, you are halfway through becoming a proficient Q developer!

Lambda World Programming Meetups are sponsored by Xebia Functional (formerly 47 Degrees) and are a subset of the Lambda World Conference. This Lambda World Programming meetup is in collaboration with ScalaMAD and MadridJUG.
Рекомендации по теме
Комментарии
Автор

Great talk! Some of the solutions for listed problems in J from same language family:

NB. 1st, factorial, comments start with NB.
! NB. this is a bit of cheating since J has factorial primitive

*/ @: (1 + i.) NB. in J you can omit arguments, @: is a compose operation, we first do 1 + i. which is range similar to til shifted by 1, then multiply-reduce/fold it.
NB. this is direct definition equivalent, y is a reference to right argument
{{ */ 1 + i. y }}

NB. 2nd mutations
p2 =: {{ +/ x ~: y }} NB. here x refers to left argument you would invoke this as 'ABCDEFG' p2 'ABXDYFG' to get 2, J uses infix notation, =: is assignment
[: +/ ~: NB. tacit solution that does not mention arguments

NB. 3rd longest sequence
iota =: i.@:# NB. for an array make a sequence 0, 1, ... <array length>, # finds array length
unique_id =: (- iota)@:(# iota) NB. we first find indexes of non-zero elements, then subtract another iota to compute unique id per group
p3 =: {{ >./ #/.~ unique_id y }} NB. /. is a key operation that groups inputs by value, it is also combinator similar to fold /, here we use # to count elements for each group

NB. 4th pyramid
p4 =: <./~@:(}:, |.)@:(1 + i.) NB. this one slightly shorter in J, first bracket computes familiar range, |. reverses array and }: removes last element

p4 5 evaluates to
1 1 1 1 1 1 1 1 1
1 2 2 2 2 2 2 2 1
1 2 3 3 3 3 3 2 1
1 2 3 4 4 4 3 2 1
1 2 3 4 5 4 3 2 1
1 2 3 4 4 4 3 2 1
1 2 3 3 3 3 3 2 1
1 2 2 2 2 2 2 2 1
1 1 1 1 1 1 1 1 1

RomanKotelnikov