Skip to content

Instantly share code, notes, and snippets.

In this tutorial we're going to build a set of parser combinators.

What is a parser combinator?

We'll answer the above question in 2 steps.

  1. What is a parser?
  2. and, what is a parser combinator?

So first question: What is parser?

Scaling your API with rate limiters

The following are examples of the four types rate limiters discussed in the accompanying blog post. In the examples below I've used pseudocode-like Ruby, so if you're unfamiliar with Ruby you should be able to easily translate this approach to other languages. Complete examples in Ruby are also provided later in this gist.

In most cases you'll want all these examples to be classes, but I've used simple functions here to keep the code samples brief.

Request rate limiter

This uses a basic token bucket algorithm and relies on the fact that Redis scripts execute atomically. No other operations can run between fetching the count and writing the new count.

@BobDickinson
BobDickinson / TextField.swift
Created April 11, 2016 23:16
Multi-line UITextView with background and placeholder functionality of UITextField
// This class is necessary to support "inset" (required to position placeholder appropriately
// in TextView)
//
class TextField: UITextField
{
var inset: UIEdgeInsets = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0);
override func textRectForBounds(bounds: CGRect) -> CGRect
{
return UIEdgeInsetsInsetRect(bounds, inset);
@erica
erica / gist:999dabc1a2d176bce3ec
Last active February 11, 2017 01:40
Composed character count
extension String {
var composedCount : Int {
var count = 0
enumerateSubstringsInRange(startIndex..<endIndex, options: .ByComposedCharacterSequences) {_ in count++}
return count
}
}
@boopathi
boopathi / README.md
Last active August 28, 2023 14:35
Creating a Swift-ReactNative project

Settings

  1. Create a project in XCode with the default settings
    • iOS > Application > Single View Application
    • Language: Swift
  2. Under project General settings, add ReactKit to Linked Framework and Libraries
    • + > Add Other... and choose /path/to/react-native/ReactKit/ReactKit.xcodeproj
  3. Now ReactKit would have been imported. Link it by choosing it from the list.
    • + > lib.ReactKit.a
  4. Under project Build Settings,
import Foundation
extension String
{
var length: Int {
get {
return countElements(self)
}
}
@staltz
staltz / introrx.md
Last active May 3, 2024 13:00
The introduction to Reactive Programming you've been missing
@ctalkington
ctalkington / Gemfile
Last active May 16, 2023 20:19
Nginx, Sinatra, and Puma.
source :rubygems
gem "puma"
gem "sinatra"
@coopermaruyama
coopermaruyama / vendor-ffmpeg-heroku
Created October 27, 2012 08:39
Install FFMpeg on heroku (Rails)
## Get FFMpeg working on heroku by building binaries using vulcan
gem install vulcan
vulcan create foo
git clone --depth 1 git://source.ffmpeg.org/ffmpeg
cd ffmpeg
@leostratus
leostratus / webkit-pseudo-elements.md
Created September 21, 2012 01:44
Webkit Pseudo-Element Selectors (Shadow DOM Elements)

An ongoing project to catalogue all of these sneaky, hidden, bleeding edge selectors as I prepare my JSConf EU 2012 talk.

Everything is broken up by tag, but within each the selectors aren't particularly ordered.

I have not tested/verified all of these. Have I missed some or got it wrong? Let me know. - A

A friendly reminder that you may need to set this property on your target/selected element to get the styling results you want:

-webkit-appearance:none;