Hi, I want to learn Clojure, my idea is to learn by reading programming Clojure, Alex Miller, and practicing with Exercism. Any other hints? Also, like when you are learning programming for the first time and people tell you to do a calculator, what kinds of programs are good for a beginner to learn Clojure concepts?
I find simple games are a great way to get started - TicTacToe, Rock Paper Scissors, etc. You can practice making an abstraction and implementing one or more computer-based ones (random guess, some strategy, etc) and a human one that handles interaction with the user
the game itself is naturally modeled as a loop
I recommend a chess computer ememy or a raytracer or a small command line tool for something which you always wanted to have.
Thank you, you gave me good ideas!
I was playing around with a toy problem today and I'm wondering if there's a general pattern for this kind of thing. I had a problem in which I was able to write some reducing functions (fn of state,val->state), which was really nice. That kind of implies that each value is being "consumed" by the reduction to produce the next state. But what if you have almost a reducing function: something that takes state,val->[state, val], as in the state is updated, but so is the value. Imagine for example my state is a bag accumulating sets of items. My bag might only be able to fit part of a set, and so the result is the updated bag (including some more stuff) and a smaller set than was input. Maybe it's fine that the "shape" of the function isn't a reducing function in this situation, but it's so close that it feels like maybe I'm just missing something obvious
i normally model this as larger state. The state is two things: inventory and canโt fit in inventory. or discard. Thereโs also a more complicated pattern with coeffects perhaps?
logically, I do as @dpsutton suggests. For efficiency, you may want something like https://aphyr.com/posts/360-loopr-a-loop-reduction-macro-for-clojure if the processing is happening all at once.
a reduce is a state machine, where state machines are a function from state, input to new state, the class of machines that have two outputs like state, input to new state, output are confusingly called transducers
Interesting. Thanks. I was thinking of wrapping the state, but that had other points of tension with the surrounding code and the abstraction in general. But maybe it's an indicator that I got the boxes wrong. I'll try inlining some stuff and see if a different factoring falls out the other end
There's only so many words in computer science I guess, even when we use the seemingly esoteric ones ๐
There's also a connection to be had here with https://en.wikipedia.org/wiki/Mealy_machine, where you have a 'step function' of the form (S, I) -> (S, O). Although strictly speaking I think the automata theory requires those state spaces to be finite
Apparently this pattern is called 'mapAccum' in the Haskell world :) https://hoogle.haskell.org/?hoogle=(s%20-%3E%20i%20-%3E%20(s%2C%20o))%20-%3E%20s%20-%3E%20%5Bi%5D%20-%3E%20(s%2C%20%5Bo%5D)
Don't try to define a mental structure for everything in advance. Just regard [state val] as a state, and then everything fits into your mental model. The state does not have to look always identical. The state can be these two things.