This file contains hidden or 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
switchx = (value, fns...) -> return ret for fn in fns when ret=fn value | |
whenx = (pred, fn) -> (val) -> (fn val) or true if pred val | |
defaultx = (fn) -> (val) -> fn val | |
# example | |
jump = (p) -> p.takeEnergy JUMP_ENERGY | |
canJump = (p) -> p.getEnergy() > JUMP_ENERGY | |
# ... |
This file contains hidden or 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
# Very simple implementation for generators in CoffeeScript. Obviously not as powerful as real generators, | |
# and only useful for iterating over values (pulling values via `.next()` is quite impossible to implement | |
# with a natural `yield` syntax in the generator), but still quite useful and provides an elegant syntax. | |
# The `generator` function is used as a decorator for functions. The function passed to `generator` gets | |
# called with the `this` context having a `yield` property that should be used to yield values to consumers. | |
# The function returned by calling `generator` should than be called with a function to consume the values. | |
generator = do -> | |
StopIteration = {} |
This file contains hidden or 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
# Just a fun experiment I hacked together :-) | |
## Basic handling | |
results = current = {} | |
group = (format) -> (name, func) -> | |
previous = current | |
(current.sub ||= []).push current = name: format.replace '%s', name | |
try | |
previous.beforeEach?() | |
do func |
This file contains hidden or 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
# Just playing around with some ideas... | |
interface = (names...) -> (klass) -> throw new Error "Missing #{n}" for n in names when not klass::[n]?; klass | |
extender = (props) -> (klass) -> klass::[k]=v for k,v of props; klass | |
watchable = interface 'watch', 'unwatch' | |
writable = interface 'write' | |
evented = extender | |
on: (event, callback) -> do stuff | |
trigger: (name, args...) -> do stuff |
This file contains hidden or 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
/* | |
* Just some small UI enhancement for multi-projects. In my setup, I have an `project` custom field and | |
* "project/" prefixes for the component, version and milestones fields (e.g. "AwesomeProject/1.2" as a, | |
* version, "SomeProject/User" as a component, etc). This JavaScript hides the irrelevant components/versions/ | |
* milestones on the ticket creation/edit forms according to the selected project, adds some navigation | |
* to the roadmap page to only show milestones of a specific project and creates links for the project-specific | |
* roadmap page (using the hash). | |
*/ | |
This file contains hidden or 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
# Allows return values to be returned from anonymous functions within other functions. The function that | |
# the value should be returned from should be decorated with the `catchx` function, and the value should | |
# be returned using the `returnx` function. This is done by throwing the return value using `returnx`, | |
# than catching it inside the decorator and `return`ing it. | |
# Was previously part of a specific solution for iterators at https://gist.github.com/1953057 | |
catchx = returnx = null | |
do -> | |
ReturnValue = (@val) -> |
This file contains hidden or 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
# With a `returnex` function decorator for returning exception values | |
# Previous versions at https://gist.github.com/1922656 | |
iter = returnex = null | |
do -> | |
StopIteration = {} | |
NextIteration = {} | |
ReturnValue = (@val) -> | |
helpers = | |
continue: -> throw NextIteration |
This file contains hidden or 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
# A tiny helper for creating objects with dynamic keys. I found myself repeating the following pattern all over my code: | |
doStuff = -> | |
obj = {} | |
obj[k] = foo for k in bar() | |
obj | |
# Which is not very elegant, and in many cases forces me to expand one-line functions into multiple lines. | |
# Instead, its possible to move the repeated pattern into a separate function: |
This file contains hidden or 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
# Adds a new `default_cols` option under [query] with a comma-separated list of default columns | |
# See http://trac-hacks.org/wiki/DefaultColsPlugin | |
from trac.core import * | |
from trac.web.main import IRequestFilter | |
from trac.config import ListOption | |
class DefaultCols(Component): | |
implements(IRequestFilter) |
This file contains hidden or 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
# A simple implementation to get a private storage area for objects. | |
# Also allows setting default properties for the storage area. | |
# Written in CoffeeScript: (with an added `export` function. On the regular version, use `exports.privates = ...`) | |
export privates = do -> | |
counter=0 | |
(proto) -> | |
key = "__id#{ ++counter }" | |
store = [] | |
(object) -> store[object[key] ||= store.length] ||= Object.create proto or null |