Skip to content

Instantly share code, notes, and snippets.

View craftybones's full-sized avatar

Srijayanth Sridhar craftybones

  • Bangalore, India
View GitHub Profile

From Hardcoded Squares to Flexible Patterns

The humble checkerboard, with its alternating squares of light and dark, holds surprising depth when it comes to programming. Generating this familiar pattern can be tackled in various ways, each showcasing fundamental programming concepts and revealing their strengths and limitations. Let's embark on a journey through code, using the checkerboard as our canvas, to explore these concepts and discover the art of building flexible and efficient solutions.

A simple printf

This section presents the simplest approach to generating a checkerboard pattern using the printf function. Here, we directly print hardcoded strings representing alternating rows of "1"s and "0"s.

#include 

Motivation

A lot of us Clojurians make the claim that programming in Clojure has provided us unique perspectives on computing. We further claim that these perspectives make us better programmers in other languages too.

this tweeter wanted concrete examples.

While I might not be able to make a case that it makes us better programmers, I'd like to provide a small catalog of example that illustrates the difference in perspectives.

Functions, not operators

rapheljr

TODO

  • Refer README.md
  • Look up todo frequently
  • Add -n+num and -c+num
  • Add -r and -q
  • Make extraction from tailMain
(defn length-between? [x y]
(fn [password] (<= x (count password) y)))
(def alpha-only? (partial re-matches #"^\p{Alpha}*$"))
(def digit-only? (partial re-matches #"^\p{Digit}*$"))
(def alpha-numeric? (partial re-matches #"^\p{Alnum}*$"))
(def special-char? (partial re-find #"[^\p{Alnum}]"))
(def classifiers {:weak [(length-between? 0 5)
(some-fn alpha-only? digit-only?)]
const firstAlgo = (x) => {
if (x % 15 == 0) {
return { numberOfChecks: 1, val: "fizzbuzz" };
}
if (x % 5 == 0) {
return { numberOfChecks: 2, val: "buzz" };
}
if (x % 3 == 0) {
return { numberOfChecks: 3, val: "fizz" };
}
(defn make-set-of-positions
[positions]
(->> positions
(mapcat second)
(into #{})))
(defn create-game
"Creates a game consisting of players and their ship positions.
positions is expected as a map {:carrier #{1 2 3 4 5}, :destroyer...}"
[player-1-positions player-2-positions]
// Run on node repl
// In node, type in
// .load factorial.js
// editor
// This side will be pure lambda calculus
const identity = (x) => x;
const constantlyFirst = (x) => (y) => x;
(ns extras.core
(:require [reagent.core :as r]
[clojure.string :as string]))
(defn fibo
([] (fibo 0N 1))
([a b] (lazy-seq (list* a (fibo b (+ a b))))))
(def fruits ["apple" "grapes" "banana" "mango"])
(def colors (repeatedly #(rand-nth [:red :green :black :blue])))
(def fruit-seq (map vector (cycle fruits) colors))
#include <stdlib.h>
#include <stdio.h>
#include "filter.h"
IntArray *create_int_array_from(int *values, int length)
{
IntArray *array = (IntArray *)malloc(sizeof(IntArray));
array->values = (int *)malloc(sizeof(int) * length);
array->length = length;
for (size_t i = 0; i < array->length; i++)
(defn take-while-adjacent [pred]
(fn [rf]
(let [prev (volatile! ::none)]
(fn ([] (rf))
([result] (rf result))
([result input]
(let [pv @prev]
(if (or (identical? pv ::none) (pred @prev input))
(do
(vreset! prev input)