Skip to content

Instantly share code, notes, and snippets.

extension Dictionary where Value: Equatable
{
func allKeys(for value: Value) -> [Key]
{
return self.enumerated().compactMap {
$0.element.value == value ? $0.element.key : nil
}
}
}
#include <stdio.h>
int sum0(int n)
{
return n > 0 ? sum0(n - 1) + n : 0;
}
int sum1(int n)
{
return n > 0 ? sum1(n / 10) + (n % 10) : 0;
extension String
{
func character(at index: Int) -> Character?
{
guard index <= self.count && index >= -self.count && index != 0 else {
return nil
}
return Array(self)[(index > 0 ? index - 1 : self.count + index)]
}
@tjhv
tjhv / winsockserver.cpp
Created February 17, 2018 05:35
Old snippet from the beginning.
#include <iostream>
#include <winsock.h>
#define PORT 7096
#pragma comment(lib, "wsock32.lib")
int main(void)
{
std::cout << "Start of program." << std::endl;
WORD wVersion = MAKEWORD(1, 1);
WSADATA wsaData;
@tjhv
tjhv / ircbot.c
Created February 17, 2018 05:34
Old snippet from the beginning.
#include <netdb.h>
#include <stdio.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <unistd.h>
#define DSTADDR "darklite.be.eu.SwiftIRC.net"
#define DSTPORT 6667
@tjhv
tjhv / winsockircbot.c
Created February 17, 2018 05:33
Old snippet from the beginning.
//feb 1 2009
#include <stdio.h>
#include <string.h>
#include <windows.h>
#include <winsock.h>
int main(int argc, char * argv[])
{
int eid;
@tjhv
tjhv / echoserver.c
Created February 17, 2018 05:29
Old snippet from back in the day.
/*
* @Author tjhv
* @Version 1.0.1
* @year 2009
**/
#include <arpa/inet.h>
#include <netinet/in.h>
#include <stdio.h>
@tjhv
tjhv / ircbot.php
Created February 17, 2018 05:27
Old snippet from back in the day.
//jan 21 2009
<?php
class Client
{
var $server;
function server($dstaddr=NULL)
{
if (!$dstaddr)
{
@tjhv
tjhv / JavaNIOServer.java
Last active February 17, 2018 05:26
Old snippet from back in the day.
/**
* @author tjhv
* @version 1.2, 09/18/2008
* @since 1.0
*/
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.channels.ServerSocketChannel;
@tjhv
tjhv / btree.c
Created February 17, 2018 05:23
Example Binary Tree.
//simple btree done as an exmaple
//not all null pointers checked, incomplete EXAMPLE
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#define myassert(x, y) printf(y); assert(x);
//btree consists of a search, insert and delete.