F# has an interesting feature called Active Patterns. I liked the idea and started thinking how I would implement this in C#. It all started from this StackOverflow question to which only Scala answers were given at the time.
Yeah, if you read the Microsoft definition you can almost see the egghead that wrote that so that you can't understand anything. Let's start with a simple example that I have shamelessly stolen from here.
// create an active pattern
let (|Int|_|) str = match System.Int32.TryParse(str) with | (true, int) -> Some(int) | _ -> None
// create an active pattern
let (|Bool|_|) str = match System.Boolean.TryParse(str) with | (true, bool) -> Some(bool) | _ -> None
// create a function to call the patterns
let testParse str = match str with | Int i -> printfn "The value is an int '%i'" i | Bool b -> printfn "The value is a bool '%b'" b | _ -> printfn "The value '%s' is something else" str
// test
testParse "12" testParse "true" testParse "abc"
The point here is that you have two functions that return a parsed value, either int or bool, and also a matching success thing. That's a problem in C#, because it is strongly typed and if you want to use anything than boxed values in objects, you need to define some sort of class that holds two values. I've done that with a class I called Option<T>. You might want to see the code, but it is basically a kind of Nullable class that accepts any type, not just value types.
Then I wrote code that did what the original code did and it looks like this:
var apInt = new Func<string, Option<int>>(s => { int i; if (System.Int32.TryParse(s, out i)) returnnew Option<int>(i); return Option<int>.Empty; }); var apBool = new Func<string, Option<bool>>(s => { bool b; if (System.Boolean.TryParse(s, out b)) returnnew Option<bool>(b); return Option<bool>.Empty; });
var testParse = new Action<string>(s => { var oi = apInt(s); if (oi.HoldsValue) { Console.WriteLine($"The value is an int '{oi.Value}'"); return; } var ob = apBool(s); if (ob.HoldsValue) { Console.WriteLine($"The value is an bool '{ob.Value}'"); return; } Console.WriteLine($"The value '{s}' is something else"); });
It's pretty straighforward, but I didn't like the verbosity, so I decided to write it in a fluent way. Using another class called FluidFunc that I created for this purpose, the code now looks like this:
var apInt = Option<int>.From<string>(s => { int i; return System.Int32.TryParse(s, out i) ? new Option<int>(i) : Option<int>.Empty; });
var apBool = Option<bool>.From<string>(s => { bool b; return System.Boolean.TryParse(s, out b) ? new Option<bool>(b) : Option<bool>.Empty; });
var testParse = new Action<string>(s => { FluidFunc .Match(s) .With(apInt, r => Console.WriteLine($"The value is an int '{r}'")) .With(apBool, r => Console.WriteLine($"The value is an bool '{r}'")) .Else(v => Console.WriteLine($"The value '{v}' is something else")); });
Alternately, one might use a Tuple<bool,T> to avoid using the Option class, and the code might look like this:
var apInt = FluidFunc.From<string,int>(s => { int i; return System.Int32.TryParse(s, out i) ? new Tuple<bool, int>(true, i) : new Tuple<bool, int>(false, 0); });
var apBool = FluidFunc.From<string,bool>(s => { bool b; return System.Boolean.TryParse(s, out b) ? new Tuple<bool, bool>(true, b) : new Tuple<bool, bool>(false, false); });
var testParse = new Action<string>(s => { FluidFunc .Match(s) .With(apInt, r => Console.WriteLine($"The value is an int '{r}'")) .With(apBool, r => Console.WriteLine($"The value is an bool '{r}'")) .Else(v => Console.WriteLine($"The value '{v}' is something else")); });
As you can see, the code now looks almost as verbose as the original F# code. I do not pretend that this is the best way of doing it, but this is what I would do. It also kind of reminds me of the classical situation when you want to do a switch, but with dynamic calculated values or with complex object values, like doing something based on the type of a parameter, or on the result of a more complicated condition. I find this fluent format to be quite useful.
One crazy cool idea is to create a sort of Linq provider for regular expressions, creating the same type of fluidity in generating regular expressions, but in the end getting a ... err... regular compiled regular expression. But that is for other, more epic posts.
The demo solution for this is now hosted on Github.
Here is the code of the FluidFunc class, in case you were wondering:
Cibola Burn is the book that worried me the most. James S.A. Corey had created a world in which the Solar System has been colonized and Abaddon's Gate, the third book in the Expanse series, had ended with humanity gaining access to one thousand new star systems. I liked the Solar System background and I really thought the fourth book was gonna suck. Well, while being some of the same old thing as the other books and maybe even better written - so a better book - it also sucked because I could easily imagine Picard and The Enterprise going on a mining colony to settle a territorial dispute and, beside being PG-13, having almost nothing changed.
The plot of the book is about Holden and the Rocinante being sent to mediate a situation between the representatives of an Earth corporation and the people who had landed on the planet before the corporation had even filed a claim. You have your familiar characters like the crew of the Rocinante and Miller and even Havelock (Miller's former partner, now a security employee of the Earth corporation), you have your psychotic leader types that mess everything up while the good guys hesitate to just shoot them, you have the very human characters with children that need to be saved, you have the overwhelming but dumb alien presence and the snowballing crisis that drives it all. I thought the story was a bit of a rehashing of the same ideas and therefore I enjoyed it less than I would have if I had read it standalone. I know that successful series are based on successful books and must present kind of the same so to not alienate its readers, but as the intergalactic situation changes dramatically, damn if I don' feel the plot should vary a little, too.
Given that science and technology have always played a big part in the Expanse, you get to see more attention to the details than from other authors, but so far Cibola Burn felt to me like the least scientifically accurate so far. And yet I liked it, because it is well written and it drives the reader through the story and makes most characters likable and one wonders what the hell would they do if they were in the character's shoes.
Cibola is the Spanish transliteration of a native name for a pueblo (Hawikuh Ruins) conquered by Francisco Vásquez de Coronado, also one of the seven mythical gold cities that the conquistadors searched for in vain.
I've been monitoring more closely the access to my blog and I noticed that a lot of people are interested in the post about the Sicilian Wing Gambit, defined as pushing b4 in reply to the standard Sicilian Defense e4 c5. So I will be trying in this to use new knowledge and computer engines to revisit this funky opening gambit. As such I will be using LiveBook, a system created by the people at ChessBase that tries to catalog and discover chess based on active chess games and analysis, as well as computer engines, in this case Komodo 9 with a 256MB table memory. I've continued each variation until there was only 1 game left in the database, then I stopped.
Main line from LiveBook
Let's start with LiveBook. Here is a PGN with the main variations in order of use. You will notice that the main line is to accept the gambit (GM Jan Gustafsson even wrote "take the pawn and be happy!" at that particular junction), then refuse the second pawn and immediately challenge the center - which would have been the Sicilian idea all along - by pushing d5. It loks a bit like a Scandinavian Defense, but without White being able to push the Black queen back with Nc3. The main line shows Black gaining advantage, but then losing it by move 12, where equality sets in. However, the computer does not recognize some of the moves in the main line as best.
In the line that I was interested in, the one where Black takes the pawn on the a-file, White gains the classical center and technically it is ahead in deployment of minor pieces, if one considers a knight on the a-file and a semi blocked in bishop developed pieces. However, not all is lost, as the computer has some ideas of its own. Also keep in mind that the Sicilian Wing Gambit is not well known and few people actively employ it.
Now let's put Komodo on the job, let us know what is going on here. Many people analysed the position resulting after pushing b4 and with depths of 36 and 40, computer engines overwhelmingly suggest taking the pawn. However we might want to explore what happens if we take another option. It is interesting to note that Komodo 9 pushes the main move as the third most important at depth 24. Perhaps later on this would get reversed again, but this soon into the game it just tells us that the other options are equally good. The two moves I am talking about is d5 and e5. Interestingly enough, the second most common human move (b6) is not even on the radar for the computer, while the computer move appears to have been played only 4 times by humans. So let's take a look at computer moves:1. e4 c5 2. b4 d5 3. exd5 cxb4 4. a3 Qxd5 5. Nf3 e5 6. axb4 Bxb4 7. c3 e4 8. cxb4 exf3 9. Qxf3 Qxf3 10. gxf3 * At the end of all this, White has four pawn islands and doubled pawns, but can quickly use the semi open files to attack with rooks. Maybe this discourages you, but remember two things: these are computers making these moves and while the position looks weird, you get attacking chances with no loss of material. That is the purpose of a gambit after all.
Computer analysis: accepting both pawns
Let's see what computers say about the line that we want to happen. The gambit is accepted, the a-pawn is captured as well. What then? I was surprised to see that, depending on depth and engine, the next move is quite different. Stockfish 6, at depth 39 goes with d4, taking control of the center and ignoring the Black a-pawn. The variations from this position are quite complex and have less to do with this gambit. I would gamble (pardon the pun) that the purpose of the wing gambit was reached at this point. Computers give a clear equality between players, but remember that even after we capture the a-pawn, we have still would be a pawn down. Black is forced to passive moves like e6, d6, having to spend resources to regain center control, while most White pieces have clear attack lines.
But what happens in between these two options? What if Black accepts the gambit, but doesn't take the second pawn? Will the computer see the same result as in the "human main line" we first discussed? Not quite. The computer moves are really different from the human ones. 1. e4 c5 2. b4 cxb4 3. a3 e5 4. Nf3 Nc6 5. Bb2 Nf6 6. Nxe5 Qe7 7. Nf3 Nxe4 8. Be2 d5 9. O-O Qd8 10. Bb5 bxa3 11. Nxa3 Bc5 * The result is another equal position, where White lost the center, but has a strong, yet weird development.
Also check out 3... d5: 1. e4 c5 2. b4 cxb4 3. a3 d5 4. exd5 Qxd5 5. Nf3 e5 6. axb4 Bxb4 7. c3 e4 8. cxb4 exf3 9. Qxf3 Qxf3 10. gxf3 Ne7 * An interesting tactic is not to take the d5 pawn and instead advance the e-pawn to e5: 1. e4 c5 2. b4 cxb4 3. a3 d5 4. e5 Nc6 5. Bb2 Qb6 6. Nf3 Bg4 7. axb4 Qxb4 8. Bc3 Qe4+ 9. Be2 Bxf3 10. gxf3 Qf4 11. d4 * You can watch an example game in this variation from Kingscrusher. My opinion on this is that White forces a strong center, but, as seen from the computer variation, the sides get seriously compromised. The truth is that I always wondered if there is a solid play with the king in the center. This might be it, although keep in mind that in that position White is a pawn down.
What if we start with b4 and then try to move towards the center?
Well, that's easy to answer: it's another opening :) called the Polish or Sokolsky opening and I have written another blog post about it, although it is pretty old. Maybe I will also revisit that one. The point with that opening is that it already shows Black what we plan and it has some other principles of work, more closely related to the English opening to which it sometimes transposes. The Wing Gambit, though, is a response to the Sicilian, trying to pull the opponent from their comfort zone and into ours.
What if we delay the b4 push?
One can wait for the wing gambit until knights have left their castle. That's called the Portsmouth Gambit (1. e4 c5 2. Nf3 Nc6 3. b4) and some consider it stronger than the gambit presented here. It might be interesting to analyse. Haven't found a lot of resources on it, just this 2014 book from David Robert Lonsdale.
Another option is to play 3. a3, preparing a support of b4 on the next move. It does produce similar results as the base gambit, but I didn't have time to analyse it and it feels a bit slow, to be honest.
When Black defends with b6
Defending c5 with b6 leads to a Sicilian without the b-pawns. That means that an attack on the queen side is out and the White light square bishop can linger around on the queen side as long as it wants, targeting that juicy Black king side from afar. Combine that with the dark square bishop having a nice diagonal as well. 1. e4 c5 2. b4 b6 3. bxc5 bxc5 4. Nf3 Nc6 5. Bb5 Nd4 6. Nxd4 cxd4 * For an example of that variation, check out Kingscrusher's video.
Traps in the Sicilian Wing Gambit
I couldn't find a lot of traps in the Sicilian Wing Gambit. One good video on this comes from GJ_Chess (ignore his Indian accent, he is actually quite good and, what I like a lot, he focuses on traps and dirty tricks in his videos):
Other ideas and nomenclatures
The ECO category for this opening is B20, same as for the Sicilian Defense, which doesn't help a lot.
After Black captures the b-pawn, c4 is called the Santasiere variation of the Wing Gambit, and Black's only option seems to try for control of d4 with either e5 or Nc6. Taking en-passant is giving White a lot of compensation in development.
The goading of the Black pawn with a3 that we've covered above is called the Marshall variation. If Black pushes the pawn to d5 and White captures, we enter the Marienbad variation, while if Black captures the a-pawn, it is called Carlsbad variation. For the record, bad is the German name for bath not an indication that the variation is bad :).
Pushing a3 before b4 is called the Mengarini gambit. Check out a game from 2013 between Dobrov and Blom.
Other resources on the gambit
A nice review of this gambit, with names of the variations and some human analysis, can be found on chess.com. (The same author has a post on The Portsmouth Gambit as well)
On chesstempo there is a nice list of games with this variation that you can search. Play around with the Advanced Search parameters. As a reference, there are 11 games from people over 2500 using the Wing Gambit and White has most wins. If restricting to games after 2000, you only get one, which ended in a draw, although White was better at the end. You can see the game here: Timur Gareev vs Gata Kamsky, US Championship (2015).
Unlike the Sokolsky opening, the purpose of the Sicilian Wing Gambit is not to push the b-pawn to block Black's development, but to deflect the c-pawn from protecting the center. The goal is reached when White has a strong center. In no way does it mean it is a winning gambit. There are no brutal traps, no quick wins, the only purpose of this opening is to pull an aggressive Sicilian player from their comfort zone and into a slower, more positional one. That means that the White player needs to attack like crazy until Black is a mere smear on the board, otherwise the center control and speed advantage that may be obtained from the opening can be easily lost.
From my analysis I gather that Black should accept the gambit, but not continue to take pawns like the a-pawn, instead focusing on their own piece development and control of the center. With perfect computer play, equality is reached and maintained. White often fianchettoes the dark square bishop to b2 from where they put pressure on the Black king side. Black's game often centers on exchanging pieces, so that the opening advantage gets lost. The best chance Black has to decline the gambit seems to be pushing d5, going into murky territory.
White on the other hand should push for the center, even propping the d-pawn with c3 and blunting the dark square bishop diagonal. Then focusing on attack is the most important feature, as in most gambits.
Careful with the variation in which Black attacks the e4 pawn with d5, then capturing with the queen after the exchange. If not careful the queen can fork the king and the rook. That is why Nf6 is played by White as the next move or even Bb2, although that's not as good, as the bishop can be deflected.
Usually the a-pawn is recaptured with the knight, not the bishop. This may seem surprising, but what it prepares is moving the knight on b5, attacking c7 and a7 and being very hard to dislodge, as the a-pawn is pinned to the rook. Some variations sacrifice the Black rook in the corner for a quick counter-attack. In case it is captured with the bishop, the idea is to exchange dark square bishops and prevent the Black king from castling.
In several games I have seen, moving towards the center forces Black to use e6 followed by d5, to which White can respond with e5 themselves and get into French defense territory. Personally I dislike playing against the French, but in this case, without b-pawns, the theory is quite different as well. For an example, check out this video from Kingscrusher.
Even if caustic GM Roman Dzindzichashvili categorized this as the worst opening for White, don't forget that it was used by Fischer in 1992 to beat Spassky. Well, a transposition thereof. If you are confident in your chess skills, this is just as good an opening as any other and at least you need to know it a little in order to defend against it.
I would love some comments from some real chess players, as all of this is based on game databases and computer analysis. Please leave comments with what you think.
Video examples
Here is Simon Williams using the gambit against a young Polish player:
ChessTrainer shows a nice game where he uses the gambit to get a quick center and take his opponent out of Sicilian main lines:
MatoJelic is showing us some classical games: Thomas A vs Schmid, Hastings 1952
Coma is my favorite Romanian bands and I've known them almost since they were formed. They have been singing for 16 years now and it was nice to see the concert room filled with people of all ages, including a 16 year old boy who had his birthday on the same day. For me this concert was a double whammy, as the lead singer of one of the opening bands is a former colleague of mine. Yeah, small world.
The opening bands where Till Lungs Collapse and Pinholes. TLC were nice, with my boy Pava almost collapsing his lungs. Pinholes were a bit strange: from five people on the stage, only the drummer didn't sport a guitar. Their writing process must be weird. Then Coma came on stage, at about 0:00 and played for an hour an a half. They were great! I've been to many of their concerts and this is one of the best yet. The band's "curse" struck again, on Dan Costea's acoustic guitar, but they were able to continue without it with no problems. They sang all time favorites, some newer songs, they also did Morphine, which is one of my personal favorite songs of theirs. I wish they would have managed to squeeze Daddy in there, or at least 3 Minute.
Catalin Chelemen was on fire, Dan was doing his usual PR thing and he was great as well and it seemed like they all had a good chemistry with the new guitar player, Matei Tibacu. Well, new for me. Unfortunately the sound in Fabrica was pretty bad. While inside you could kind of focus on the right notes, especially if you knew what the songs were supposed to play like, if you try to gauge the quality of the concert from the videos that are online now, you want to mute it almost instantly. People were respectful enough not to smoke during the concert (I can't wait for the smoking ban to come in effect!), but my clothes still smelled of tobacco when I got home, from people smoking in the next room.
As far as I know you can hear them next at the Electric Castle Festival, July 14-17, with so many other great bands. I am tempted to go there, but I am not one for festivals. Great job, Coma, and good luck!
Click here to see some nice photos from the concert.
The authors known as James S.A. Corey have planned the series of books The Expanse to have three volumes. As such, Abaddon's Gate feels like a wraparound of the stories so far, while also remaining a good standalone feature. However, because of the overwhelmingly positive response for the series, it was continued for another three volumes, and now three more are announced. There are also various novellas in between books. That is a problem, since this book ends up undoing what the first two started. But let's not get ahead of ourselves here, just be warned that this review may contain spoilers, without which I couldn't possibly comment on the plot of the book.
If you are only interested in my general opinion of the book, I believe it is consistent with the quality of the second. There are more characters, but also less compelling ones. There is a great mystery, but a rather bland one. There is a danger, well... several of them in succession, but they feel a bit artificial, just to keep the tension going. I am not complaining, but I am also not thrilled. As in Caliban's War, there are several characters that seem put there just to annoy me. There is this lesbian preacher that always needs to save the souls of everybody around, for example; she kind of felt like someone nagged the writers to put more progressive characters in, like writing about giant alien artifacts in space is not progressive! Fortunately, she is also important to the plot, so she is not just added there like condiment on food, yet the parts of her philosophizing about the meaning of God bored me to tears. Then there is a psychotic captain that doesn't seem to be a person at all. He just randomly appears and does stuff, and I am not the only one noticing this. And there are more, but I don't feel the need to complain that much. Here be spoilers!
The story revolves around a ring like structure that the alien "protomolecule" has constructed outside the orbit of Uranus. A random ship goes through revealing it is in fact a wormhole. An entire fleet of ships gets in, for various reasons, and again Holden and his crew are in the middle of it all. Yet their roles are quite limited up until very close to the end of the book, the main character here being the sister of Juliette Mao who seems to be seriously unhinged, moving from dangerous psycho killer to kind person who wants to fix things. Quite a lot of psychos in this book. The end opens up a wormhole hub, thus allowing access to the stars. And that is what I take offense with.
You see, the beauty of the series, as made clear by the TV show actually and less by the books, is that it presents a plausible solar human occupation, something close by, that we could achieve in about a century or so, given the magical fusion Epstein drive. It goes into the social, the economic, the political, less in the technical, but still quite a lot. It brings hope. Then there is this alien thing that we don't understand which throws a wrench in our understanding of our place in the Universe. So much to explore there (unintended pun, I assure you). Yet, the end of the third book in The Expanse opens up the stars, even more magically than the Epstein drive, ending the promise of a realistic hard science fiction universe and going towards the implausible and yet so overused "humanity among the stars" trope. I really hope they don't fuck this up for me!
Bottom line: I feel like this book had flaws in its characters, while the story was kept ablaze just by random dangers and conflicts that did not engage me as a reader. Yes, I wanted to see how it all unfurls, but I couldn't care less about the people involved. While it certainly has kept me entertained and it is a good book, I couldn't help begrudging this as well as the ending, which for me ended the promise of exploration and colonization of the Solar system, while going into that all too trodded interstellar medium (hearing me about it it seems like it is seething with stuff, but I mean the literary medium).
P.S. Abaddon was a gate associated to the realm of the dead from the Hebrew Bible.
Hi, all! I have been working on the blog lately and I want to know what you think of the new format. Changes include: infinite scroll in the main page for more than 800px width, being able to choose in Tools your own color theme or custom font - including for dyslexia, better thumbnail support for list pages, style improvements, testing for various browsers. Let me know by commenting, using the chat or open the blog and start typing...
The second book of The Expanse is much better written than the first, however the story is a little weaker. There are more of the details one would already be familiar with from the TV show, the character of Chrisjen Avasarala is introduced and chapters are written from the perspective of many more characters than just Miller and Holden. That means there are many more chances to completely dislike a chapter if you hate the character. For me, that character was Prax, someone who would endanger everybody and himself with random emotional outbursts. Perhaps he was put there just to offset the slightly similar behavior of Holden, who now looks like the paragon of professionalism in comparison.
The plot revolves around yet another alien infestation, this time on Ganymede, only it is not clear who or what is actually responsible. The already angered Mars and Earth navies use this as a pretext to attack each other, while the Rocinante crew find themselves agents of the OPA, sent to find out what happened. As opposed to Leviathan Wakes, the book adds two major female characters, Avasarala and Bobbie, a Martian marine, and a lot of the story is about their interaction. In truth, what the Rocinante does on Ganymede is almost inconsequential until they make contact with the two women, which felt like the main characters of the book, if I had to choose. This is also a sign of a better built world, in which one has to struggle to identify the lead characters.
While the book was clearly better (some even suggest it is the best in the series), I didn't find myself attracted so much by the story. Instead of a mystery, like in the first, we pretty much discover from the beginning what is going on and only halfheartedly root for the characters to get in the same position the reader has been all along. I like Avasarala's character, but in order to show how badass she is, we have to go through all the political machinations in the UN which I couldn't care less about. Bobbie was slightly more interesting, but she starts off with such low confidence that until she gets to embrace her role there is so much filler. And Prax... don't get me started on that asshole! He is central to the Rocinante crew quickly finding out what has happened, but for the rest of the book he just drags along. He would have been a perfect character to be killed off, adding to the darkness of the tale.
And I think this is where the books and the TV series diverge the most. While the show is perfectly content to show a dark, hopeless world, the books fight to maintain some sort of feeling of normalcy, of hope, leading to reasonably happy endings. The show recognizes that in a Solar System built on exploitation, armed spaceships and politics there is no hope for the little man, there is no silver lining, there are just people trying to survive while colossal forces push them around like leaves in the wind: another reason to watch the series, at least the first season, before starting reading the book.
Bottom line: Caliban's War, by James S.A. Corey, contains no reference to a character called Caliban, which is a Shakespeare character. In the play The Tempest, Caliban is a part-human monster and slave who rebels against his masters. Even starting from the title, the authors spill the beans on what the story is all about. While I enjoyed the book and now I am reading the third one in the series, I can't help wishing they would have maintained the tension and mystery of Leviathan Wakes.
I am a .NET programmer living and working in Bucharest, Romania.
Posts are divided into programming and misc.
Check out the icons above on how to contact or chat with me.
Unless otherwise specified, all code or any type of work you find on this blog is under MIT license. While I welcome attribution, I don't require it. Just use anything in any way you see fit. Consider it completely and utterly shared for the lulz.