Skip to content

Instantly share code, notes, and snippets.

View alexandream's full-sized avatar

Alexandre Araujo Moreira alexandream

View GitHub Profile
@alexandream
alexandream / join-strings.c
Last active January 20, 2017 08:40
Join strings by a separator using varargs.
#include <stdio.h>
#include <stdarg.h>
#include <string.h>
#include <stdlib.h>
static
size_t compute_joined_length(const char* separator,
const char* first,
va_list argp) {
size_t seplen = strlen(separator);
@alexandream
alexandream / encode-crlf.c
Created January 12, 2017 02:10
Basic set of functions to encode/decode line breaks in a string. Useful when trying to store program outputs in a text/plain line-based serialization format.
/* This is a quite simple set of functions that turns multi-line strings in
single line strings with the line breaking characters backslash escaped.
The encode_crfl does the following transformation.
transform : character -> string
transform '\n' = "\\n"
transform '\r' = "\\r"
transform '\\' = "\\\\"
transform c = c
@alexandream
alexandream / word-wrap.c
Last active January 11, 2017 11:19
A simple function to word wrap a C string to a specific maximum line length
/* As an example, when using a line width of 25, the string:
"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."
Becomes:
"Lorem ipsum dolor sit
amet, consectetur
adipiscing elit, sed do
eiusmod tempor incididunt
@alexandream
alexandream / pretty-printer.scm
Created October 10, 2013 01:18
Simple scheme pretty printer in Guile.
#!/usr/bin/guile -s
!#
(use-modules (ice-9 pretty-print))
(define args (command-line))
(if (< (length args) 2)
(begin
(display "Not enough parameters, need a file name.")
(newline))
@alexandream
alexandream / constructor.cpp
Created April 22, 2012 20:40
Print 1 to 100 without loops or conditionals
#include <iostream>
int n = 1;
struct Printer {
Printer() {
std::cout << n++ << std::endl;
}
} array[100];
int main(int argc, char ** argv) {
@alexandream
alexandream / RecursionException.java
Created April 22, 2012 20:35
Print 1 to 100 without loops or conditionals
public class RecursionException {
// Print and loop through recursion, throw exception when n reaches zero.
public static void loop(int n, int limit) {
int x = 1/n;
System.out.printf("%1$d\n", (limit - n) + 1);
loop(n - 1, limit);
}
public static void print(int n) {
loop(n, n);
@alexandream
alexandream / Lambdas.java
Created April 22, 2012 20:32
Print 1 to 100 without loops or conditionals
public class Lambdas
{
public static void main(String args[]) {
Printer p = new Printer();
times10(times10(p)).call();
}
// The "add" function returns the function that executes the parameters f and g
// one after the other.
//
@alexandream
alexandream / print100-lambdas.js
Created April 22, 2012 20:24
Print 1 to 100 without loops or conditionals
// This creates a function that will print n and increment it on every call.
function make_printer() {
var n = 1;
return function() {
console.log("%d\n", n++);
}
}
// The "add" function returns the function that executes the parameters f and
// g one after the other.
@alexandream
alexandream / print100-lambdas.scm
Created April 22, 2012 20:21
Print 1 to 100 without loops or conditionals
;; This creates a function that will print n and increment it on every call.
(define (make-printer)
(let ((n 1))
(lambda ()
(display n)
(newline)
(set! n (+ n 1)))))
;; The "add" function returns the function that executes the parameters f and g
;; one after the other.