Your issue here seems to be specifically around Clojure's sequences which can be infinite.
Clojure's sequences can either be null or implement an interface called ISeq, which among other things has 2 particularly important functions, first
and next
.
Consider the following java class
public class LongsFrom implements ISeq{
private Long _currentNumber;
public LongsFrom(Long start){
_currentNumber = start
}
public Object first(){
return _currentNumber;
}
public ISeq next(){
return new LongsFrom(_currentNumber + 1);
}
}
It represents an infinite sequence of increasing numbers starting wherever you want. As you keep calling next() on it, you keep realizing more and more of the sequence, however all you really need to keep in memory is whatever the latest object in the sequence is.
What cycle does is create an instance of a class which implements the ISeq interface (it knows how to calculate the sequence but doesn't actually do it until you start calling next). Then your take
command actually starts calling first
and rest
on this class to get however much of the sequence it needs.
A class which actually performs cycle
would only be slightly more complicated than my LongsFrom example. The cycle class could have 2 private variables, one which holds the list of things it needs to cycle through and then an integer to remember which index it's currently on. first would return the item at the current index. next would return a new cycle object with the exact same internal list, but an index of 1 + currentIndex (unless we're at the end of the list in which case we start the index back at 0)
The reason why you have memory issues if you don't have the take
command is that if you don't have the take
command, the repl tries to print
the sequence and the default implementation for print
is to keep calling first
and rest
until the sequence ends (which is never in the case of cycle
)