Skip to content

Instantly share code, notes, and snippets.

@jirkapenzes
Last active August 29, 2015 14:14
Show Gist options
  • Save jirkapenzes/56f8670bf9ff4eb1e938 to your computer and use it in GitHub Desktop.
Save jirkapenzes/56f8670bf9ff4eb1e938 to your computer and use it in GitHub Desktop.
TopMonks coffee - presentation samples
(defprotocol Event
(scheduled-time [this])
(execute [this]))
(defn simulate [event]
(synchronize-time (scheduled-time event))
(execute event))
public interface Event {
public double getScheduledTime();
public void execute();
}
// ...
public void simulate(Event event) {
synchronizeTime(event.getScheduledTime());
event.execute();
}
(defn work [initial-task]
(loop [tasks (list initial-task)]
(when (not-empty tasks)
(let [task (peek tasks)]
(recur
(into (pop tasks) (execute taks)))))))
public static void work(Task initialTask) {
ArrayList<Task> tasks = new ArrayList<Task>();
tasks.add(initialTask);
while (!tasks.isEmpty()) {
Task task = tasks.remove(0);
List<Task> newTasks = task.execute();
tasks.addAll(newTasks);
}
}
(defn sum-even-fibonacci [limit]
(loop [first 0
second 1
sum 0]
(if (<= second limit)
(let [new-sum
(if (= (mod second 2) 0)
(+ sum second)
sum)]
(recur second (+ first second) new-sum))
sum)))
(println "Result: " (sum-even-fibonacci 4000000))
public static void main(String[] args) {
int limit = 4000000;
int first = 0;
int second = 1;
int sum = 0;
while (second <= limit) {
if (second % 2 == 0) {
sum += second;
}
int newFirst = second;
int newSecond = first + second;
first = newFirst;
second = newSecond;
}
System.out.println("Result: " + sum);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment