Skip to content

Instantly share code, notes, and snippets.

@danielkcz
danielkcz / examples.ts
Last active July 15, 2021 02:49
Children handling with React FC
const NoChildrenAllowed: ReactFC<NoChildren> = () => null
const RequireSingleChild: ReactFC<Required<SingleChild>> = () => null
type TProps = Required<SomeChildren> & {
otherProp: number
}
const RequireMoreChildrenAndOtherProps: ReactFC<TProps> = () => null
@kharrison
kharrison / Country.swift
Last active February 16, 2021 15:35
Swift Hash Functions
import Foundation
struct Country {
let name: String
let capital: String
var visited: Bool
}
extension Country: Equatable {
static func == (lhs: Country, rhs: Country) -> Bool {
use std::path::PathBuf;
use std::collections::{BTreeMap, HashMap, HashSet};
use std::thread;
use std::os::raw::c_void;
use std::rc::Rc;
use std::cell::{Cell, RefCell};
use std::i16;
use std::time::Instant;
use failure::{err_msg, Error};
@sebmarkbage
sebmarkbage / react_legacyfactory.md
Last active March 15, 2020 00:32
Use a factory or JSX

React Element Factories and JSX

You probably came here because your code is calling your component as a plain function call. This is now deprecated:

var MyComponent = require('MyComponent');

function render() {
 return MyComponent({ foo: 'bar' }); // WARNING
@prendio2
prendio2 / SUPTableViewController.m
Created March 26, 2014 15:05
Custom viewWillApear to restore selected row when transition is cancelled
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
NSIndexPath *selectedRowIndexPath = [self.tableView indexPathForSelectedRow];
if (selectedRowIndexPath) {
[self.tableView deselectRowAtIndexPath:selectedRowIndexPath animated:YES];
[[self transitionCoordinator] notifyWhenInteractionEndsUsingBlock:^(id<UIViewControllerTransitionCoordinatorContext> context) {
if ([context isCancelled]) {
[self.tableView selectRowAtIndexPath:selectedRowIndexPath animated:NO scrollPosition:UITableViewScrollPositionNone];
@bitwalker
bitwalker / config.ex
Created July 19, 2016 23:00
Useful config wrapper for Elixir
defmodule Config do
@moduledoc """
This module handles fetching values from the config with some additional niceties
"""
@doc """
Fetches a value from the config, or from the environment if {:system, "VAR"}
is provided.
An optional default value can be provided if desired.
@mattpodwysocki
mattpodwysocki / eventsource.js
Last active September 23, 2019 15:34
Adding support for server-sent events for RxJS
if (!!root.EventSource) {
/**
* This method wraps an EventSource as an observable sequence.
* @param {String} url The url of the server-side script.
* @param {Observer} [openObserver] An optional observer for the 'open' event for the server side event.
* @returns {Observable} An observable sequence which represents the data from a server-side event.
*/
dom.fromEventSource = function (url, openObserver) {
return new AnonymousObservable(function (observer) {
@shavit
shavit / broadcast_udp.swift
Created November 14, 2016 03:03
Send a message using UDP in Swift 3
//: Playground - noun: a place where people can play
import Cocoa
import Darwin
func htons(value: CUnsignedShort) -> CUnsignedShort {
return (value << 8) + (value >> 8)
}
let INADDR_ANY = in_addr(s_addr: 0)
@ningsuhen
ningsuhen / Regex.swift
Last active April 27, 2019 18:39
Swift extension for Native String class to support Regex match and Regex replace. Credit - http://www.swift-studies.com/blog/2014/6/12/regex-matching-and-template-replacement-operators-in-swift
import Foundation
struct Regex {
var pattern: String {
didSet {
updateRegex()
}
}
var expressionOptions: NSRegularExpressionOptions {
didSet {
@c0gent
c0gent / main.rs
Created July 28, 2016 15:59
Glium - instancing over a vertex buffer slice
//! An example demonstrating that glium has a problem using instancing when
//! the vertex buffer is a slice with a non-zero starting index.
//!
#[macro_use] extern crate glium;
use glium::{DisplayBuild, Surface};
// Vertex Shader:
static VERTEX_SHADER_SRC: &'static str = r#"
#version 140