Skip to content

Instantly share code, notes, and snippets.

View CTMacUser's full-sized avatar

Daryle Walker CTMacUser

View GitHub Profile
@CTMacUser
CTMacUser / zipWith_demo.cpp
Created March 18, 2012 21:32
Improved version of my sample code from <http://stackoverflow.com/a/9736489/1010226>.
// Taken from the final code I provided as an answer to a StackOverflow query
// at <http://stackoverflow.com/a/9736489/1010226>.
// Copyright 2012 by Daryle Walker
#include <algorithm> // min
#include <cassert> // assert
#include <cmath> // pow
#include <cstddef> // size_t
#include <iostream> // cout
#include <iterator> // back_inserter, begin, end
@CTMacUser
CTMacUser / times.cpp
Created April 2, 2013 04:04
A custom literal suffix that returns an object that takes a function (object) for generic looping. C++11 required. The fact that the idea is theoretically and actually possible is amazing. I don't know if that's good or bad, yet.
/*
Copyright (C) 2013 by Daryle Walker
Based on posts off the Boost mailing lists on 2013-Jan-31 by Tim
Blechmann and TONGARI. C++11 is required; according to Live Work
Space's code-runner (on 2013-Apr-1), only Clang >= 3.2 and
GCC >= 4.7 accept it.
The original post (Tim Blechmann) of the 7 in the thread:
http://article.gmane.org/gmane.comp.lib.boost.devel/238310
@CTMacUser
CTMacUser / slice.cpp
Created April 3, 2013 00:07
An example program to create a variadic function that computes a chain of indexing expressions. It's a solution of a problem I posted at http://stackoverflow.com/q/10171525/1010226
/*
Demostration of variadic function template that runs a series of indexing
expressions. Based of a problem I posted at
http://stackoverflow.com/q/10171525/1010226
Copyright (C) 2013 Daryle Walker
I'm letting anyone use this code under the Boost Software License.
*/
#include <cassert>
#include <cstddef>
@CTMacUser
CTMacUser / remove_some_extents.cpp
Created April 25, 2013 22:07
Use when a built-in array types needs extents to be stripped, but the amount to strip is between one and all (exclusive). I thought I needed this for another project, but I ended up not needing it (yet). So it's here so I won't forget, and for posterity.
// Copyright 2013 Daryle Walker.
// Distributed under the Boost Software License, Version 1.0. (See a copy at
// <http://www.boost.org/LICENSE_1_0.txt>.)
#ifndef REMOVE_SOME_EXTENTS_HPP
#define REMOVE_SOME_EXTENTS_HPP
#include <cstddef>
@CTMacUser
CTMacUser / AdjacentDifference.swift
Last active March 23, 2016 22:30
Using Swift to demonstrate finding powers with just adding (and some prep work).
import Foundation
/**
A generator for the distance between two adjacent elements from another generator.
*/
public struct AdjacentDifferenceGenerator<Source: GeneratorType where Source.Element: Strideable>: GeneratorType {
/// The last value read, needed to calculate the difference
private var queue: (Source.Element?, Source.Element?)
@CTMacUser
CTMacUser / Mismatch.swift
Created July 6, 2016 00:40
Proposal and Code to add C++'s mismatch algorithm to Swift.
extension CollectionType {
func diverges<PossiblePrefix: CollectionType where PossiblePrefix.Generator.Element == Generator.Element>(from possiblePrefix: PossiblePrefix, isEquivalent: (Generator.Element, Generator.Element) throws -> Bool) rethrows -> (Index, PossiblePrefix.Index) {
var selfIndex = startIndex
var fromIndex = possiblePrefix.startIndex
while selfIndex != endIndex && fromIndex != possiblePrefix.endIndex {
let areEqual = try isEquivalent(self[selfIndex], possiblePrefix[fromIndex])
guard areEqual else {
break
}
@CTMacUser
CTMacUser / NNNN-local-object-aliases.md
Last active June 28, 2017 16:53
Swift proposal for object aliases
@CTMacUser
CTMacUser / NNNN-alternative-types.md
Last active July 29, 2017 05:45
Swift proposal for strong type-aliases

Alternative Interface Value Types

Introduction

This proposal adds a nominal type whose structure is an existing value type and interface can be customized, including selective use of the original type's interface. It is equivalent to the type declaration feature in Go, newtype in Haskell, and the oft-requested "strong typedef" in C(++).

@CTMacUser
CTMacUser / NNNN-entity-duplication.md
Last active August 8, 2017 16:23
Swift proposal for a "macro" for generating comma-separated lists.

Entity Duplication

Introduction

This is a proposal to add a macro-like facility to automatically create lists.

@CTMacUser
CTMacUser / Stern-Calkin-Wilf-sequences.swift
Created January 1, 2018 22:21
Stern's Diatomic and the Calkin–Wilf Sequences in Swift
// Copyright (c) 2018 Daryle Walker.
// Distributed under the MIT License.
// Based off <https://en.wikipedia.org/wiki/Calkin–Wilf_tree>.
// "fusc" from Edsger W. Dijkstra.
// To-Do: Add the iterative version of "fusc"
struct SternDiatomicSequenceIterator: IteratorProtocol {
var cache = [0: 0, 1: 1]