“Rule 1. The sum of a signed value and an unsigned value of the same size is an unsigned value.”
“If developers are allowed to write "to the hardware,” the result is a broken platform where the vendor can’t move forward without breaking the apps.“
Apple added closures support to C, C++ and Objective C with lightweight threading and multicore balancing.
x = ^{ printf(“hello world\n”); }
Snow Leopard shows a rare case when upgrade brings you more valuable performance and bug fixes rather then incredible new features.
“Writing a Mandelbrot set calculator in a high level language is like trying to run the Indy 500 in a bus. While it might be amusing for a car magazine to test circuit times for various busses on a race course, it really tells potential bus buyers very little about which bus they should buy as the problem of cost efficiently maximizing transportation throughput on normal roads involves different tradeoffs than the problem of no-expense-spared maximization of transportation speed on an extremely windy road.”
W3C box model (width specifies pure content area width) represents bottom-up philosophy where content specifies the area for itself. Parent elements should adapt to it.
IE6 box model (width specifies content area with borders and paddings) represents top-down philosophy where designer specifies available spaces for nested elements.
In fact, content designers don’t care about the container width and html designers think better in terms of IE6 box model. Thanks to stupid standards, we all have to make additional calculations in our heads when working with styleshits.
According to previous note on interfaces in dynamically typed languages, it would be great if the API could specify type expectations even easier than a to_type message send to each argument in method body.
class Person
def initialize(name.to_s, birthday.to_date = DEFAULT_DATE)
...
end
end
Should be interpreted as:
class Person
def initialize(*args)
args.size == 2 or raise ArgumentError
name = args[0].to_s
birthday = (args[1] || DEFAULT_DATE).to_date
...
end
end
This idea can be applied to other languages as well.
Why’s that? Consider Dostoevskiy’s “Crime and Punishment”: it is just 1 Mbyte. And what size is your codebase?
Object interface is a set of messages with defined behavior the object should respond to.
In statically typed languages, interface is required by type declaration. In dynamically typed languages this is done by telling object of unknown type to cast itself to the desired type.
# User's code expects object responding to #to_page
# when casted to page, we expect proper #render and #size behavior
class SomeController
def process(object)
page = object.to_page
page.render
page.size
end
end
# 1. Page class with #to_page interface returning self
class Page
def to_page
self # return self since it is already a page
end
# page public api
def render
end
def size
end
end
# 2. Non-page class with #to_page interface
class AtomicBomb
def to_page
# return a relevant article
Page.wikipedia_article("Nuclear weapon")
end
end
# 3. Class without #to_page, but with #render method
# fails with "object does not respond to #to_page" exception
# and does not cause undesirable side effects
class Foo
def render
# very specific nasty rendering method
end
end
# 4. Class without #render
# fails with "object does not respond to #to_page" exception
# rather than with less descriptive "object does not respond to #render"
class Baz
end
This technique minimizes duck typing collisions by reducing the number of exposed methods to a single “to_{unique_type_name}” method. It also protects you from inventing obtrusive method names with type prefixes such as “page_render” or “page_size_in_characters” (see example above).
The rule of thumb:
1. When API consists of more than one method, introduce #to_my_type method
2. Whenever you receive an object from an unknown source (e.g. defined in a different file), use explicit type casting with #to_some_type method.
Note: never ever make others ask about the kind of an object using #respond_to?. This method should be used for legacy code and indicates possible duck typing issues.
Also, #is_a?(SomeAbstractInterfaceModule) is considered badly designed compared to #to_* methods.
class BlankSlate
class <<self; alias __undef_method undef_method; end
alias __instance_eval instance_eval
ancestors.inject([]){|m,a| m + a.methods }.uniq.
each { |m| (__undef_method(m) rescue nil) unless m =~ /^__/ }
end
class MyProxy < BlankSlate; end
Note 1: ancestors.inject{…} ensures that all Kernel methods like :p are properly removed.
Note 2: alias __instance_eval could be safely removed if you don’t need this method.
“Giving every piece of data a fixed identity, is radically different from the relational model which deals only with sets of values and leaves the notion of identity up to the application. Working with identities as a first-class notion is essential if schema is to be flexible. Long before we can agree on the exact shape of the data used to represent a person or a building, we can agree that individual people or buildings exist and that they have certain obvious attributes that we might want to record: height, address, builder, etc.”
JavaScript transactional memory extension
“The name, if you’re wondering, comes from the simplest sequence of operations which will thoroughly mix the bits of a value - "x *= m; x = rotate_left(x,r);” - multiply and rotate. Repeat that about 15 times using ‘good’ values of m and r, and x will end up pseudo-randomized"
Austin Appleby.
HFS+ also has a few specific optimizations. When a file is opened on an HFS+ volume, the following conditions are tested:
— The file is less than 20 MB in size
— The file is not already busy
— The file is not read only
— The file is fragmented (the eighth extent descriptor in its extend record has a non-zero block count)
— The system uptime is at least 3 minutes
If all the above are satisfied, the file is relocated (de-fragmented) - on-the-fly.
”—What Is Mac OS X?, Mac OS X Filesystems (Amit Singh)Thanks to gotsyk for the link.
Thanks to julik for the link.
The encoding scheme is required to
— Be human readable and machine readable.
— Be compact. Humans have difficulty in manipulating long strings of arbitrary symbols.
— Be error resistant. Entering the symbols must not require keyboarding gymnastics.
— Be pronounceable. Humans should be able to accurately transmit the symbols to other humans using a telephone.
The parser enables you to write BNF-like rules directly in JavaScript without need to compile the file (like with Ragel, YACC, Bison, ANTLR etc.)
The grammar is a JS function with 11 arguments (9 rules and 3 hooks). Each rule gets two arguments: text (string) and state (arbitrary value) and returns a tuple of new text and new state (or null if rule was not matched). Parser walks character by character from left to right. text is always a tail of the initial text. Generally, text is empty string when parser finishes.
All(rule1, rule2, …) — a sequence of rules. Example: All(Char(“[”), list, Char(“]”)) defines a JS array.
Any(rule1, rule2, …) — “OR” rule for any number of rules. Example: JSONValue = Any(StringRule, ObjectRule, ArrayRule, …)
Char(alphabet) — character matching rule. Example: digit = Char(“0123456789”)
NotChar(alphabet) — inverse of Char(). Any character — NotChar(“”).
Optional(rule) — tries to match rule and skips it if not matched. Example: optSpace = Optional(Char(“ \t\r\n”))
EOF — matches the end of the text. Fails if text != “”.
Terminator — terminates parser. That is, always returns empty text.
Y — Y-combinator for defining recursive rules. Example: X = Y(function(x) { return abc(x) } ), where x is equal to X. Google for more info on Y-combinator.
Hooks enable you to actually build some structures using your grammars. Every hook returns a new state value to use in further rules. You should avoid mutable state values because some rules may be thrown away if not matched later (remember: this is a recursive parser!). For example, use array.concat([value]) instead of array.push(value).
Capture(rule, function(buffer, state1){ return state2 }) — captures raw string buffer to store in the state2.
Before(rule, function(state1){ return state2 }) — creates a new state for the rule (e.g. creates empty array for array syntax).
After(rule, function(beforeState, afterState){ return newState }) — creates a new state after successful match. You can put nested values to the container if beforeState is a container before rule parsing, afterState is a nested value (after rule match) and newState is a new container with this value.
See usage examples in JSONGrammar.js
See the parser source code in Parser.js
How to use Natural Order
Drop Natural Order into the System Folder and Restart your Mac.
”—NaturalOrderSort.orgA Cool And Practical Alternative To Traditional Hash Tables
More Robust Hashing: Cuckoo Hashing with a Stash
Recent advances in the theoretical literature have proposed interesting modifications to traditional hash tables. The authors of these papers propose hash tables which
a) have a guaranteed constant time for a lookup
b) have amortized constant time for an insertion
c) require table size only slightly larger than the space for the elements
Previous hash table technologies have offered at most two of these three.
(Thanks to Julik for inspiring to think on the subject.)
In Io, JavaScript and Python there’s a model of “hash-table” objects. The object contains some slots, which you access and then decide what to do with them. If the slot appears to be a function, you can call it. In JS there’s a bit of magic: interpreter knows where the function just came from, so it can specify reasonable “this” pointer for function call. In Io there’s less smart decision: upon slot access interpreter checks “activatable” property of the object (not the slot entry, but the object this slot refers to!) and activates the object if it happens to be a method. However, in Io x := y is being a message setSlot(‘x’, y), so that you can hook in.
On the other hand, Ruby has a much stronger notion of message passing: you never ever can modify inner object data (that is, @ivars) without message send. The most simple @foo update happens through the accessor method called foo=.
However, from the implementation point of view, Ruby has two kinds of hash tables per object: method table and ivars table.
Implementation is created under time and knowledge constraints. Therefore, it is full of bugs, and it becomes obsolete as soon as our knowledge evolves. What is important in technology: the idea and context behind each decision.
MyApp := Object clone do(
Foo := Object clone do(
bar := method(Bar) # Bar is not accessible from here!
)
Bar := Object clone do(
foo := method(Foo) # Foo is not accessible from here!
)
)
How can we improve that? First, lets define MyApp with Module clone rather than Object clone:
MyApp := Object Module clone do(
Foo := Object clone do(
bar := method(Bar)
)
Bar := Object clone do(
foo := method(Foo)
)
)
All we need now is to have all inner objects to have MyApp in the list of prototypes. Therefore, lets override Object slot to refer to MyApp rather than the standard Object:
Module := Object clone do(
Object := method(self)
)
See Module.io on GitHub.Yesterday I have rewritten 3600 LOC codebase in ActionScript as if there were “message eating” null in the core and the standard libraries (please see A Generalized Null Object Pattern by Nevin Pratt). I got a diff with just 100 lines removed and 180 lines updated. It is at most 7% code reduction.
It seems that “message eating” null neither produces considerably more elegant code, nor considerably more brittle code (in all cases ignoring null value is correct).
In sense of VM implementation, it might be easier to implement a classical “exception raising” null and use custom Null Pattern classes where appropriate.
Good software framework saves much more time providing explicit knowledge where to put things in, rather then providing syntactic steaks and strippers.
iovm idea: “activatable” should be a slot property rather than value property. This allows to pass methods around and access them directly by slot name in all the contexts. Current implementation makes you to use getSlot(slotName) syntax to avoid activation.
Excellent write up about “message eating” Nil.
There’s no “srand” function in AS3. That is you cannot seed random number generator.
And when you get the very same results each time swf is loaded you find this:
However, if you need true randomness in an event-driven application, you can do this:
setInterval(Math.random, 10)
Flash Player will run Math.random() in background producing different random values at different points of time.
In a regular priority queue entry with priority N is taken before all the entries with priority M (given N > M).
Sometimes, however, 3-priority entry should not beat one hundred of 1-priority items. It seems natural to introduce a double-linked list of weighted nodes, where newly inserted node can come in front of a limited number of nodes with a total weight less then a weight of a new node.
Illustration. Given a stream of nodes with weights:
[1a, 2, 1b, 3, 1c, 1d, 4, 1e]
weighted queue intermediate states would be:
[1a]
[2, 1a]
[2, 1a, 1b]
[2, 3, 1a, 1b]
[2, 3, 1a, 1b, 1c]
[2, 3, 1a, 1b, 1c, 1d]
[2, 3, 1a, 4, 1b, 1c, 1d]
[2, 3, 1a, 4, 1b, 1c, 1d, 1e]
In such queue priority “N” means skipping N items of priority 1.