Skip to content

Instantly share code, notes, and snippets.

View jakubpetrik's full-sized avatar
:octocat:

Jakub Petrík jakubpetrik

:octocat:
View GitHub Profile
@jakubpetrik
jakubpetrik / markov-algorithm.swift
Last active September 11, 2015 19:49
Markov Algorithm in Swift 2.0
/**
http://rosettacode.org/wiki/Execute_a_Markov_algorithm
Create an interpreter for a Markov Algorithm. Rules have the syntax:
<ruleset> ::= ((<comment> | <rule>) <newline>+)*
<comment> ::= # {<any character>}
<rule> ::= <pattern> <whitespace> -> <whitespace> [.] <replacement>
<whitespace> ::= (<tab> | <space>) [<whitespace>]
@jakubpetrik
jakubpetrik / naive-shunting-yard.swift
Last active September 11, 2015 20:00
Naive aproach to ShuntingYard in Swift 2.0
/**
http://rosettacode.org/wiki/Parsing/Shunting-yard_algorithm
Given the operator characteristics and input from the Shunting-yard algorithm page and tables
Use the algorithm to show the changes in the operator stack and RPN output as each individual token is processed.
*/
import Foundation
@jakubpetrik
jakubpetrik / drama.swift
Created September 23, 2015 06:30
String and print with support for a drama
extension String {
var dramatically: String { return self + ", ..." }
}
func printWithDrama(items: Any..., separator: String = " ", terminator: String = "\n") {
print(items.map { "\($0)" }.joinWithSeparator(separator).dramatically, terminator: terminator)
}
@jakubpetrik
jakubpetrik / Reducer.m
Last active September 23, 2015 07:36
Reducing BOOLs.
@interface Reducer : NSObject
+ (id(^)())reduceToBoolMultiple:(NSNumber *)boolArgs, ... NS_REQUIRES_NIL_TERMINATION;
+ (id(^)())reduceToBoolArray:(NSArray *)array;
@end
@implementation Reducer
+ (id (^)())reduceToBoolMultiple:(NSNumber *)firstArg, ... NS_REQUIRES_NIL_TERMINATION {
BOOL retVal = firstArg.boolValue;
@jakubpetrik
jakubpetrik / ordered-words.swift
Created October 1, 2015 13:29
Ordered words in Swift
/**
http://rosettacode.org/wiki/Ordered_words
Define an ordered word as a word in which the letters of the word appear in alphabetic order. Examples include 'abbey' and 'dirt'.
The task is to find and display all the ordered words in this dictionary that
have the longest word length.
(Examples that access the dictionary file locally assume that you have downloaded this file yourself.)
The display needs to be shown on this page.
*/
@jakubpetrik
jakubpetrik / tpk.swift
Created October 2, 2015 08:46
Trabb─Pardo─Knuth algorithm in Swift 2.0
/**
http://rosettacode.org/wiki/Trabb_Pardo–Knuth_algorithm
The TPK algorithm is an early example of programming chrestomathy.
It was used in Donald Knuth and Luis Trabb Pardo's Stanford tech report
The Early Development of Programming Languages.
The report traces the early history of work in developing computer languages in the
1940s and 1950s, giving several translations of the algorithm.
The task is to implement the algorithm:
@jakubpetrik
jakubpetrik / echo.swift
Last active February 27, 2016 06:02
Native shebang - shell scripting in Swift (using Swift REPL)
/**
http://rosettacode.org/wiki/Native_shebang#Swift
In short: Use the specimen language (native) for "scripting".
Usage:
./echo.swift Hello, world!
Output:
Hello, world!
@jakubpetrik
jakubpetrik / reverse-words.swift
Created September 24, 2015 06:04
Reverse words in a string.
/**
http://rosettacode.org/wiki/Reverse_words_in_a_string
The task is to reverse the order of all tokens in each of a number of strings and display the result;
the order of characters within a token should not be modified.
Example: “Hey you, Bub!” would be shown reversed as: “Bub! you, Hey”
Tokens are any non-space characters separated by spaces (formally, white-space);
the visible punctuation forms part of the word within which it is located and should not be modified.
@objc protocol TableControllerDataSource {
func tableController(_ controller: TableController, numberOfRowsInSection section: Int) -> Int
func tableController(_ controller: TableController, cellControllerForRowAt indexPath: IndexPath) -> TableViewCellController
}
@jakubpetrik
jakubpetrik / handle-signal.swift
Created September 23, 2015 21:34
Signal handling in Swift
/**
http://rosettacode.org/wiki/Handle_a_signal
Most general purpose operating systems provide interrupt facilities, sometimes called signals.
Unhandled signals generally terminate a program in a disorderly manner. Signal handlers are
created so that the program behaves in a well-defined manner upon receipt of a signal.
For this task you will provide a program that displays a single integer on each line of output at
the rate of one integer in each half second. Upon receipt of the SigInt signal (often created by the user typing ctrl-C)
the program will cease printing integers to its output, print the number of seconds the program has run,