Unknown's avatar

About ericlippert

http://ericlippert.com

Producing permutations, part one

One of my more popular articles from 2010 was my post on how to produce the Cartesian product of arbitrarily many sequences of the same type. That is, we have some sequences, say {10, 20, 30} and {200, 300, 400}, and we wish to produce the sequence of all possible combinations:

{ 
  {10, 200}, {10, 300}, {10, 400}, 
  {20, 200}, {20, 300}, {20, 400},
  {30, 200}, {30, 300}, {30, 400} 
}

A related question that I did not answer was “how do you produce all possible permutations of a sequence?” That’s the question we’re going to explore for the next few episodes.

Continue reading

That’s a big transistor

As promised last time, a fun-for-Friday rerun from the early days of FAIC. But before we get started, a quick physics refresher.

Force is the ability to change the velocity of an object by a certain amount in a given amount of time. One newton (N) of force is the ability to change the velocity of a one kilogram object by one meter per second, in one second. The earth applies a force of gravity of 9.8N on every 1kg mass near it.

Work is the application of a force to an object as it moves a certain distance. Energy is the ability to do work. One joule (J) of energy is the ability to apply a force of one newton to an object as it moves one meter. [1. Note that the force has to be applied in the direction of the motion for it to count as work; the earth’s gravitational force does no work on a sideways-moving object. This should jibe with your intuitive understanding of work; it is a lot harder to raise an object by 1 meter than it is to slide it 1 meter, where the work is done by the force overcoming friction.]

Power is the rate at which energy is consumed in time. One watt (W) of power is the consumption of one joule of energy per second.

Charge is, like mass, a fundamental property of matter. The easiest way to manipulate charge is by manipulating electrons. Charge is measured in coulombs (C). Current is the movement of charge and is measured in amperes (A); one ampere is one coulomb of charge moving past a given point in one second.

Electric potential, better known as voltage, is to charge as gravity is to mass, and is measured in volts. Applying a potential of one volt to one coulomb of moving charge consumes one joule of energy. A better way to think about it though is to divide both sides of that equation by time and get that one volt times one amp is one watt.[2. volts times amps equals watts is one of the funamental equations you have to know in order to wire a house safely. If your house power is providing a potential of 120V and your light bulb is consuming 120W of power then the current on the wire supplying the bulb is 1A. Since lighting circuits typically use wires that are safe for up to 15A, this puts a limitation on how many bulbs you can have on one circuit.]

Resistance is the tendancy of an electric conductor to resist the movement of charge, and is measured in ohms (Ω). If there is a conductor where the difference in voltage between the two ends is one volt, and the resistance is one ohm, then there will be a one amp current in the conductor.


A few years back a bunch of my coworkers and I got to discussing the space program over lunch. Someone asked why it is that we continue to launch devices into orbit by strapping a big old tank full of liquid oxygen to the device and then set it on fire. Why haven’t we developed better technology using magnets or something?

Continue reading

Building a tabletop coilgun

coilgunAs you probably know, I’ve been re-running some of my fun non-computer posts from the last decade. This Friday I’m going to rerun my post on the impracticalities of large-scale coilguns, and I thought that as a precursor to that I might talk a bit about tabletop coilguns. So, no programming language design this week.

A couple years ago my friend Morgan expressed an interest in learning about electronics so I thought that a homemade coilgun would of course be a perfect gift for a ten year old. Yes, I am that awesome avuncular figure who gets kids pocket knives and drum sets and coilguns for their birthdays. Parents, you’re welcome!

This is a great project to teach kids about circuits because it has all of the basic parts except transistors, and each part has a clear purpose. I’ve deliberately left the voltages, capacitances, resistances and inductances I chose off the circuit diagram above. Better to work them out for yourselves on the basis of what kinds of voltages sources and what capacitors you’ve got available, and what your appetite for destruction is.

Continue reading

Thumbs up

ebertRoger Ebert was — is — a hero of mine. I’ve often thought that if I could be one tenth as clear, witty, insightful and kind, I’d consider myself a good writer.

I remember getting a copy of Microsoft Cinemania in the early 1990s, casually browsing the archive of Ebert reviews and realizing that here was someone who really, really knew how to write — and from that point on I was hooked. In the decades since then I don’t think I’ve rented a movie or gone to the theater without checking to see what Ebert had to say about it first. On the few occasions when he wrote something that moved me to respond directly he always answered my emails thoughtfully and kindly. That he took the time at all out of his incredibly busy life to answer fan emails is just one small example of his generosity.

Tonight I’m going to take AV Club reviewer Zack Handlen’s advice and watch Citizen Kane again with Roger’s commentary track. It’ll be good to hear his voice.

What does the langversion switch do?

The C# compiler has a /langversion switch that is rarely used; most people do not know what it is for, and if asked to guess, most guess wrong. So let me disabuse you of your wrong guess right away: the purpose of the langversion flag is not to decide what C# language version to use. It is not a “go into compatibility mode” switch or a “use a previous version of the compiler” switch. The only effect of the langversion switch is to put the compiler in a mode in which use of features from a version of the language higher than the given version cause the compiler to emit an error.

Let me illustrate with a hilariously contrived example:

Continue reading

Monads, part thirteen

My erstwhile Microsoft colleague and parallelism guru Stephen Toub just did what I did not do last time: he applied the same treatment I gave to the maybe monad and sequence monad to the task comonad. Check out his awesome blog article!

OK, for reals this time, that’s it for the monad series. Next time on FAIC: ever wonder what the langversion switch on the C# compiler is for? Me neither. But next time you’ll find out anyway.

Monads, part twelve

Holy goodness, this series got a lot longer than I expected. I was hoping to get to some actual category theory, but let’s just talk a bit about LINQ and then wrap up. Maybe I’ll do a series on the basics of category theory in the future.

I’ve said several times that SelectMany is the bind operation on the sequence monad, but of course there are several overloads of the SelectMany extension method of IEnumerable<T>. The one that is bind is as we previously discussed:  (Error checking removed for clarity.)

public static IEnumerable<R> SelectMany<A, R>(
  this IEnumerable<A> items, 
  Func<A, IEnumerable<R>> function)
{
  foreach(A outer in items)
    foreach(R inner in function(outer))
      yield return inner;
}

Now you might think that when you say

from outer in items
from inner in function(outer)
select inner

this translates directly into a call to

items.SelectMany(outer=>function(outer))

This is not what actually happens.

Continue reading

Monads, part eleven

I had intended to spend this episode on the history of the relationship between SelectMany and the LINQ query expression syntax, but before I do that I want to briefly address a point raised in the comments of the previous episode.

We know that the key bits of the monad pattern are (1) the ability to construct a monad value that “wraps” an underlying value, and (2) the ability to apply a function from A to M<R> to a value of type M<A>. The first operation is traditionally called unit. The second operation is traditionally called bind, but is called SelectMany in LINQ.

We saw how you could build Select out of only SelectMany and a helper method that effectively just calls unit — and indeed, you can build a Select for any monad because all you need is the unit and bind; this operation is traditionally called fmap.

I also showed how you could build Where — an inefficient Where to be sure — out of SelectMany and a helper method, but reader “Medo” reasonably pointed out that there is no general monadic where because my implementation actually required three operations: unit, bind and make an empty sequence.

This criticism is entirely justified; though every monad has an analog of the sequence monad’s Select and SelectMany, not every monad has an analog of Where.

Continue reading

Monads, part ten

Last time on FAIC I gave some examples of some simple “associate extra data with a value” monads. Though those are useful, the real power of the monad pattern is when it is used to produce objects that represent workflows on data. The best example of that I can give you in C# is the sequence monad, IEnumerable<T>. The whole point of LINQ, in fact, is to make it easier to construct the monadic workflows that are better known as “queries”.

Continue reading

Monads, part nine

Last time in this series I discussed the standard terminology used for the monad pattern: that the simple construction method is traditionally called “unit”, and the function application method is traditionally called “bind”. I also pointed out that the two sub-patterns you see most frequently in implementations of the monad pattern are first, association of some state with a value, and second, construction of workflows that describe the sequencing of units of work to be performed in the future. Today we’ll look at that first kind of monad in some more detail.

Continue reading