This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
From: https://old.reddit.com/r/compsci/comments/kgpjv/can_somebody_please_explain_the_difference/ | |
Standard recursive descent parsers are LL(1) parsers, yes (the 1 means that you only look at the very next token to decide which rule to apply). Because of the whole recursive descent thing, LL parsers are top-down parsers (whereas LR parsers are bottom-up). | |
Their big downside, in parsing programming languages, is in parsing infix operator expressions. For example, suppose you want to have a simple grammar that matches a language in which you can add and multiply binary digits (ignoring operator precedence for now): | |
Expr <- Expr "*" Digit | |
| Expr "+" Digit | |
| Digit | |
Digit <- "0" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <algorithm> | |
template <typename I> | |
void rotate_2d(I& a, I& b) { | |
std::swap(a, b); | |
b = -b; | |
} | |
int main() { | |
// Suppose we want to iterate over the neighbors of a cell in a 2D grid: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void* fread_all(FILE* file, size_t* out_size) { | |
const size_t BLOCK_SIZE = 32 * 1024; | |
const size_t MEDIUM_SIZE = 10 * 1024 * 1024; | |
*out_size = 0; | |
size_t cap = 0; | |
char* data = NULL; | |
while (1) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
cd ~/Library/Developer/CoreSimulator/Devices/ | |
isVaildDeviceDir () { | |
# echo $1 | |
# $1 is not directory or empty | |
if ! [[ -d $1 ]] || ! [[ -n $1 ]] ; then | |
return 1 | |
fi |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct InterspersedSequence<S: Sequence>: Sequence, IteratorProtocol { | |
var separator: S.Element | |
var iterator: S.Iterator | |
enum State { | |
case separator | |
case element(nextElement: S.Element?) | |
} | |
var state: State |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
enum CollectionMultiplicity<C: Collection> { | |
case none | |
case one(first: C.Index) | |
case many(first: C.Index, second: C.Index) | |
} | |
extension Collection { | |
func indexedMultiplicity(where predicate: (Element) -> Bool) -> CollectionMultiplicity<Self> { | |
guard let i = firstIndex(where: predicate) else { | |
return .none |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Most implementations first check if the file exists, and then do the remove. | |
// This is subject to race conditions and requires accessing the file system twice. | |
// A better solution is to ignore that particular exception. | |
extension FileManager { | |
func removeItemIfExists(at url: URL) throws { | |
func isDoesNotExist(error: NSError) -> Bool { | |
return error.domain == NSPOSIXErrorDomain && error.code == ENOENT | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
float g_epsilon = 0.002; | |
float g_max = 50.0; | |
float sdSphere( vec3 toCenter, float s ) | |
{ | |
return length(toCenter)-s; | |
} | |
float sdBox( vec3 p, vec3 b ) | |
{ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <iterator> | |
// With C++20 concepts | |
#include <concepts> | |
template<typename F, typename T> | |
concept unary_operation = std::invocable<F, T> | |
&& std::same_as<T, std::invoke_result_t<F, T>>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
; From Page 149 of "LISP" 1st edition. | |
; This code has been translated to Common Lisp | |
; from the dialect used in the book: | |
; table -> *table* | |
; (store (table n) x) -> (setf (aref table n) x) | |
; (boole 1 ...) -> (boole boole-and ...) | |
; greaterp -> > | |
; lessp -> < | |
; (quotient x y) -> (floor x y) |
NewerOlder