2010-03-27

Joey Dyser, 100 years

Perm url with updates: http://xahlee.org/Periodic_dosage_dir/sanga_pemci/Joey_Dyser_100_years.html

Joey Dyser, 100 years

Xah Lee, 2010-03-27

A beautiful song. The song name is “100 years”, the singer is Joey Dyser. I don't know who she is though. Wikipedia doesn't have a entry of her. The song seems to be around 1975.

The other day I felt so young
but now you made me 100 years
If you turn 'round, look what you've done
you'll see my eyes, you'll see my tears

My friends all say: don't cry too long
there is another love to come
They may be right, they may be wrong
But still I love you hear my song

You disappeared without goodbye
knowin' so well, you made me cry
but troubles you could never face
so you just left an empty place

The first kiss made a fool of me
it struck me but I couldn't see
For you this love was just the same
I'm asking now, am I to blame?

emacs: command to insert date or random number

perm url with updates: Emacs Misc Tips.

2010-03-27

Another short elisp tip. I needed a fast way to insert random numbers. So i wrote:

(defun insert-random-number ()
  "Insert a 4 digits random number."
  (interactive)
  (insert
   (number-to-string (random 9999))
   )
  )

If you are new to lisp... notice that elisp does not automatically convert number to string. So, number-to-string is very convenient. There's also string-to-number.

Once i defined this function, i can either give it a Keyboard Shortcut, or define a alias. I have already too many shortcuts, so i just define a alias, by “(defalias 'irn 'insert-random-number)”. Then, when i need to have a random number inserted, i just type “Alt+x irn”.

Exercise: write a command that insert current date/time. (answer can be found at: How To Update Web Feed With Elisp)

What Determines a Computer Language's Popularity?

Perm url with updates: http://xahlee.org/UnixResource_dir/writ/language_popularity.html

What Determines a Computer Language's Popularity?

Xah Lee, 2007-08-18

[Note: The following is a online forum post, in which i posit a aspect that a computer language's popularity depends on.]

Xah Lee wrote:

The Haskell community, would benefit greatly (say, as tripling its popularity or number of users), by forbidding literature associated with its teaching, of using the term Currying.

Chris Smith wrote:

Even when teaching the meaning of the standard library routines “curry” and “uncurry”?

umm... that seems unfortunate. I think things like this, effectively functions like a damnation on the language's reach.

It is not surpring, since Haskell is made by academics, who have little contact and understanding of the real, daily, average programers in the computer industry. (and Haskell wasn't intented that it might be a language for general industrial use)

Similar problem cripples the Schemers. Personally for me, it was sad. When at the dot-com era (~1998), new langs and techs like Java, perl, cgi, javascript, css, html 3!, VRML! ...etc are seething with a bright utopian future. At the time, Python is very little known. (like a niche little thing nobody knows or cares) And, whenever Python is mentioned, Scheme is often also mentioned, and tcl as well, for comparisons and potentials. (i read most of SICP and most of R5RS in 1998)

Now, it's almost a decade later. Python has grown and risen into the limelight, and is used by many major corporations. Perl is fading, but no less used than python. CGI has faded, VRML turns out stillborn. Javasript stands firm and is ever spreading like octopus. Java deservedly, got pushed into OpenSource death by Microsoft (sounds like Netscape to me, and i do hope Java and Sun Microsystems die a painful, miserable, DEATH). Pretty Home Page, technically a extremely badly designed, weak, Perl-based language (was just Perl scripts), has risen and surpassing perl to become a pillar language in today's world of web. And then, there's still Scheme, the same old fucking Scheme, with it's cons and lambdas and tail-recursions and continuations fuck nobody knows about and nobody gives a hot damn.

Who to blame? I blame the Schemers. Not because their language being technically lacking (quite the contrary), nor because they lack mature tools or compilers or libraries. (There is scsh, which by itself already sufficiently surpasses the most advanced or popular shells such as Bash and the entire unix tool bags that are pervasively used in the industry, and scsh has been around for long). if Scheme lacks, then tcl, python, php, ruby all lack big ass. Most of these lang start with 0 experience, while scheme has some 30+ years of experience. But primarily, due to the Schemers themselves with their academic drivels. Fuck Schemers.

(and consider the baby Ruby (Oh my god another language with nothing new), which is rising like a star today)

See also: Computer Language Popularity Trend.

* * *

The above thoughts gave me a insight. That is, a language's wide adoption in the industry has a lot to do with the original purpose of the language and the community it is associated with. So, Scheme, Haskell, i dont think will be widely adopted in foreseeable future. While Ruby, considered as a computer language, really offers nothing new, but due to its origin, will likely grow with far more users and libraries then Scheme or Haskell.

Likewise, consider Common Lisp. Considered with respect to computer science, really is inferior to Scheme or Haskell in many ways, but will nevertheless more popular, given its industrial background. (and as we know, in fact has garnered a lot publicity and users in recent years) (in the same way, consider emacs lisp, probably has been quietly far more popular than Common Lisp throughout their existance, despite the fact that consider as pure computer languages, elisp lacks many language features as well as libraries when compared to CL)

... not sure how this thoguht applies to erlang, Ocamel, f#. It would suggest that these, with their industrial background, will become more popular than Haskell. (if not already a fact)

... to recapitulate this thought: that a lang's primary concern and primary community dictates what new users will be using the lang. So Scheme, Haskell, have focuses on elegance and computer science research, and thus, that's what they will be. Common Lisp, Ruby, f# has focuses on practical industrial use, all things considered, thus that's how they will be. And the universe of industrial programing is far greater than academecians.

* * *

...now back to the discussions on terminology...

Does Microsoft's f#'s literature uses jargons?

That would be interesting to see, if Microsoft, in its functional lang f#'s documentation (or naming of things in the lang), takes care to not only avoiding uncessary use of jargons, but whether they also take steps to replace computer-science jargons by more practicality oriented terms.

A Exhibition Of Tech Geekers Incompetence: Emacs whitespace-mode

Perm url with updates: http://xahlee.org/UnixResource_dir/writ/emacs_whitespace-mode_problems.html

A Exhibition Of Tech Geekers Incompetence: Emacs whitespace-mode

Xah Lee, 2009-08-13

Just wanted to express some frustration with whitespace-mode.

Emacs 23, just released, has this whitespace-mode feature. It renders spaces, tabs, newlines characters with a visible glyph. This feature, is in Microsoft Word since about 1992.

This feature is important in practical ways. For example, when you work with Delimiter-separated values file format (CSV, TSV, ...) that's a common format for importing/exporting address books or spreadsheets. It's also important in whitespace-significant langs such as Python. Or, in text processing when placement of space and tabs matters in input or output.

All i wanted, is to make Space and Tab and Newline chars visible.

However, the emacs whitespace-mode does much more than that. It is designed for tech geeking control freaks to tune every aspect of white space in his source code. The mode is filled with bells and whistles. It distinguishes tabs mixed with spaces, EOLs mixed with spaces, EOLs at beginning of file, EOLs at end of file, run on spaces at end of line, lines that has nothing to do with white spaces but is simply longer than 80 chars, etc. Each of these is rendered with different foreground, background, colors, so that they cannot possibly escape the notices of control freaks.

By default, most of these are on, so that, when you turn on the mode, most reasonable clean source code become this colorful rainbow unreadable fuck.

emacs whitespace

The default rendering of whitespace-mode. Spaced out!

I tried to tune it, with my 10 years of emacs of fucking 16 hours of using per day, and 3 years of elisp coding experience. But, after a hour, it's confusion hell sans avail.

O, that Alex idiot with his emacswiki, refused to lead emacswiki into any readable state. All he can think about is my social skills. (See: Problems of Emacswiki.)

What the fuck motherfuck. Hi tech geekers, coding freaks, social science ignoramus fucks, basic economics illiterate FSF fucks, freedom abusing selfish ideologists fucks, Richard Stallman propagandist fuck, we-try-to-be-easy-to-use linuxer idioting fucks, FUCK U.


2009-11-26
emacs whitespace-mode default

A clean python code shown in whitespace-mode by default.

emacs whitespace-mode good

Python code shown in whitespace-mode with proper customization.

For how to fix it, see: How to use and setup Emacs's whitespace-mode.

Function Application is not Currying

Perm url with updates: http://xahlee.org/UnixResource_dir/writ/apply_func_examples.html

Function Application is not Currying

Xah Lee, 2009-01-28

In Jon Harrop's book Ocaml for Scientist at http://www.ffconsultancy.com/products/ocaml_for_scientists/chapter1.html

It says:

Currying

A curried function is a function which returns a function as its result.

LOL. That is incorrect.

Here are some examples of a function that returns a function as result, but is not currying.

Mathematica example:

f[n_]:=Function[n^#];
f[7][2]
(* returns 49 *)

Emacs lisp example:

(defmacro f (n) (list 'lambda (list 'x) (list 'expt n 'x) ) )
(funcall (f 7) 2)

Perl example:

sub f {$n=$_[0]; sub { $n ** $_[0]} };
print &{ f(7) } (2);

Javascript example:

function f(n) {return function (x) {return Math.pow(x,n);}; }
alert (f(7) (2));

In the above, a function returns a function, and the result function is applied to a value. They demonstrate 2 things:

  • The ability of the lang to have a function that returns a function.
  • The ability to apply a value (of type function) to a value.

These, are 2 of the features that is part of often sloppily termed as “function as first class citizens”.

However, the above are not languages that support currying, which is a feature that Haskell & Ocaml have.

So what is Currying?

Wikipedia article Currying said it best:

In computer science, currying, invented by Moses Schönfinkel and Gottlob Frege, and independently by Haskell Curry,[1] is the technique of transforming a function that takes multiple arguments (or more accurately an n-tuple as argument) in such a way that it can be called as a chain of functions each with a single argument.

Note how it says “is the technique of ...”.

To be more concrete, in the context of a given computer language, to say that it support curring, is to mean that the compiler understands the concept to certain degree. More to the point, the language is inherently able to take a function of more than one arg and deconstruct it to several functions of single arg.

To say that function returning function is Currying, is a confusion of fundamental concepts.

Mathematically, currying is the concept of deconstructing a function of multiple parameters to a composition of several functions all of arity 1.

I like Jon, because i consider majority of his argument and perspective are more correct or sensible in his trollish spats in newsgroup fighting with tech geekers. But he is really a ass, and take every chance to peddle his book. Every opportunity, he injects random opinion into discussions about how static typing or greatness of Microsoft, which paves a way for him to post a link to his book on Ocaml/F# or “study” or “speed comparison” of his site. He does this repeatedly and intentionally, about every week for the past 2 or so years, and write in a way to provoke irate responses. In the past 2 or 3 years, i have for 2 or so times without his or any one's solicitation, publicly supported him in ugly newsgroup fights (such as some serious sounding post that accuse him of spamming or or some real life threats about network abuse). However, in the past year as i have had some debates on language issues with jon, i find Jon to be a complete asshole as far as his newsgroup demeanor goes.

PS see also: A Mathematica Optimization Problem ( story of a thread where Jon started a fight with me )

2010-03-26

Emacs: What's Region, Active Region, transient-mark-mode?

Perm url with updates: http://xahlee.org/emacs/emacs_region.html

Emacs: What's Region, Active Region, transient-mark-mode?

Xah Lee, 2008-06, ..., 2010-03-25

This page is a tutorial on emacs's concept of region, active region, transient-mark-mode, for those who want to write emacs lisp commands.

Region

The last Mark position to the current cursor position is called a Region.

Once a user sets a mark in a buffer (“Ctrl+Enter” or “Alt+x set-mark-command”), a region exists. So, almost always, there exists a region in a buffer.

By convention, commands ending in the word “-region” acts on the region, regardless whether the region is active. For example: kill-region, comment-region, fill-region, indent-region. This system worked well before emacs introduced the region highlighting feature around late 1990s.

You can get the position of region beginning by the function “region-beginning”, similarly “region-end”.

transient-mark-mode, Active Region, Highlighting of Region

transient-mark-mode

Emacs has a minor mode called transient-mark-mode, introduced since emacs 19.x or earlier. It is on by default since Emacs 23. When on, it will highlight the Active Region.

Active Region

Because a region exists once a user set a mark, and always having a section of text highlighted to the cursor position is annoying, so there's a new concept of Active Region.

The active/inactive status of a region is stored the variable mark-active, and this variable can be set/unset by commands.

When is a region Active?

Typically, when set-mark-command is called, the region becomes active (highlighted). When a command is called, typically the command will set the region status to inactive. This means, when you set mark using the keyboard or the mouse, text selection become highlighted, then after you called some command that do something, the region returns inactive again (and the highlighting goes away).

What's Text Selection?

Emacs's concept of “active region” is very much similar to the modern term “Text Selection”. When a region is active, there is a text selection.

Typically, when you want your command to act on active region, do this check:

(and transient-mark-mode mark-active)

Or, use use a higher level function “region-active-p”, which does exactly the above. (lookup its inline doc)

Emacs 23 Changes

Starting with Emacs 23, transient-mark-mode is on by default, and some command behavior changed. If there is a text selection, the command act on it, else it acts on the current word, line, paragraph, buffer (or whatever is its default input).

This change is good, because users don't need to think about whether he should choose the region or non-region version of the command. The command simply act on a text selection if there is one.

Commands with this new behavior includes: fill-paragraph, ispell-word, indent-for-tab-command, comment-dwim. The number of command that are sensitive to existence of text selection will probably increase.

(info "(elisp) The Mark")

2010-03-25

Womanizer (video)

Perm url with updates: http://xahlee.org/funny/womanizer.html

Womanizer (video)

Banner

“Britney Spears - Womanizer - Parody” by “VenetianPrincess”.

I LOL.

Who's this VenetianPrincess? Apparantly, a real chick that rose to fame on youtube. Dubbed “Female Weird Al Yankovic”, and is the most subscribed female on YouTube. She made several parodies that are highly popular. Her real name seems to be Jodie-Amy Rivera. Her website is vprincess.com.

See also White and Nerdy.

and the original version by Britney Spears.

2010-03-23

Emacs Lisp: Count Words, Count Chars, Count Region

Perm url with updates: http://xahlee.org/emacs/elisp_count-region.html

Emacs Lisp: Count Words, Count Chars, Count Region

Xah Lee, 2010-03-23

A little elisp tip. Here's a short elisp i have been using since about 2006. It reports the number of words and chars in a text selection.

(defun count-region (beginning end)
  "Print number of words and chars in region."
  (interactive "r")
  (message "Counting ...")
  (save-excursion
    (let (wCnt charCnt)
      (setq wCnt 0)
      (setq charCnt (- end beginning))
      (goto-char beginning)
      (while (and (< (point) end)
                  (re-search-forward "\\w+\\W*" end t))
        (setq wCnt (1+ wCnt)))

      (message "Words: %d. Chars: %d." wCnt charCnt)
      )))

This code is largely from Introduction to Programming in Emacs Lisp by Robert J Chassell, when i was reading it sometimes in 2005. That tutorial is for people who never programed. It was quite frustrating to read, because for every sentence you are learning about emacs lisp, you have to scan some 20 pages of things you already know about programing, such as what's variables, assignment, syntax, etc. In the end, i didn't really read that book. This function is about the only thing i got out of it.

How It Works

Now let's explain about how this function works.

The function has this skeleton:

(defun count-region (pos1 pos2)
  "..."
  (interactive "r")
  ; ...
  )

This means, when you call the function with M-x, the region beginning as a integer will be fed to your variable “pos1”, and region's end will be fed to the argument “pos2”, automatically. This is caused by the line “(interactive "r")”.

The next part of the function is this:

(save-excursion
 (let (var1 var2 ...))
 (setq var1 ...)
 (setq var2 ...)
 ...
)

The “let” is lisp's way to have a block of local variables. We are going to be doing some cursor moving and searching. However, when the function count-region ended, the cursor should return to whatever its original position when user called our function. This is what the “save-excursion” does. Quote from its inline doc:

(save-excursion &rest body)

Save point, mark, and current buffer; execute body; restore those
things.
...

Now, to count the char, it is just the length of the beginning and ending position of the region. So, it is simple, like this:

(setq charCnt (- end beginning))

Now, we move the char to beginning of region, like this: “(goto-char beginning)”. The next part count the words, like this:

(while (and (< (point) end)
                  (re-search-forward "\\w+\\W*" end t))
        (setq wCnt (1+ wCnt)))

The “(< (point) end)” is for checking that the cursor havn't reached the end of region yet.

The “(re-search-forward "\\w+\\W*" end t)” means, keep moving the cursor forward by regex search a word pattern. The “end” argument there means don't search beyond the end of region. And the “t” there means don't report error if not found.

search-forward and re-search-forward are very important functions in elisp. I use them almost in all of my text processing script. If you are not familiar with them, lookup their inline doc. (use describe-function)

So, the above “while” blog, basically means keep moving the cursor and count words, until the cursor is at the end of region.

Finally, the program just print out the result, by:

(message "Words: %d. Chars: %d." wCnt charCnt)

Exercise

Try to write a version so that, when there is a text selection, count word and char in text selection, but if there's no text selection, just count the current line. You might want to read Emacs Lisp Idioms to refresh your memory about emacs's tech meaning of “region”, “active region”, transient-mark-mode.

2010-03-21

CSS “position” Tutorial

Perm url with updates: http://xahlee.org/js/css_positioning.html

CSS Position: static, relative, fixed, absolute, Examples

Xah Recommends:
Kindle
Amazon Kindle. Read books under the sun. Review

Xah Lee, 2010-03-20, 2010-10-27

This page explains the “position” element of CSS, with examples.

The “position” element has 4 possible values: static, fixed, relative, absolute. The css positioning is difficult to understand, but they are also quite powerful.

position:static

The “position:static” is the default for all tags. If you don't specify a “position”, its default value is “static” (if it hasn't inherited anything from ancestor tags). That means, the positioning of the tag's rendered box goes with normal flow.

position:relative

Use “position:relative” to adjust a tag's position relative to the parent block.

Use “top”, “bottom”, “left”, “right” to specify the offset, and “width, height” to control the size.

See: CSS “position:relative” Example.

position:fixed

Use “position:fixed” to specify the positioning of a element with respect to the window.

When a tag has “position:fixed”, that element goes into its own layer. The normal flow of tags will flow like that tag doesn't exist.

For some examples, see:

position:absolute

The “position:absolute” is a little complex. When a tag has “position:absolute”, it goes into its own layer, like “position:fixed”, but the specified offset is relative to a parent tag like “position:relative”.

To be more precise, and this is important, the offset is actually relative to the first parent tag that has a position value other than “static”. When no parent has any of “position” spec, then it is relative to the “<html>” tag. In this situation, “position:absolute” is the same as “position:fixed”.

“position:absolute” is the most useful, but also the most difficult to understand.

Example: CSS Example of Text over Image.

CSS Position Summary

“positon” ValueOwn LayerRelative To
staticnoN/A. (Normal Flow)
relativenoparent
fixedyeswindow
absoluteyesfirst parent that's one of “relative”, “fixed”, “absolute”. If none found, then it's “<html>” tag (which would be equivalent to “position:fixed”).

Overlapping and z-index

When a tag goes into a layer, it may overlap with other tags, either covering them or being covered. To control this, you can use the attribute “z-index”. Like this: “z-index:3”. The number can be negative. The larger the value, the more front it is.

Not Like Photoshop Layers

CSS layers is not like the layers in image editing software. In Photoshop or GIMP, you can create many layers, and by default, each layer is the same size of the original layer. Each layer has a unique number. You can control which layer(s) to show or hide.

Here's how CSS's layers behave:

  • In CSS, a normal HTML page is conceptually in a layer, but it doesn't have a associated z-index.
  • When you use position with values of “fixed” or “absolute”, that tag lives in its own layer. However, the layer by default does not have the same dimension as the window. Its dimention is whatever its width and height according to css's rules for a tag's dimension (including inheritance).
  • 2 layers can have the same z-index. The z-index isn't a unique number for layers. It's a number for each tag for specifying which is more front. If you specify 2 tags with the same z-index, latter tag is more front.

It is possible to create multiple layers like image editor's layers. You just set each element to have “position:fixed”, and all with the same dimension, and all offset be 0. Then, give each element a unique z-index. Then, you can use the “visibility” attribute, with values of “hidden” or “visible”.

Was this page useful? If so, please do donate $3, thank you donors!