-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added Anton's articles from the DB and from https://web.archive.org/w…
- Loading branch information
Showing
37 changed files
with
1,485 additions
and
5 deletions.
There are no files selected for viewing
This file contains 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
45 changes: 45 additions & 0 deletions
45
user/anton.tayanovskyy/20091202-net-composite-formatting-with-keyword-expansion.done
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
--- | ||
title: ".NET Composite Formatting with Keyword Expansion" | ||
categories: "websharper,f#" | ||
abstract: "" | ||
identity: "1458,74668" | ||
--- | ||
<a href="http://msdn.microsoft.com/en-us/library/txafckwd%28VS.95%29.aspx">Composite Formatting</a> is a feature of .NET framework that comes handy even for F# programmers. Yes, <c>Printf</c>-style formatting generally is much nicer with F#, but there are situations where the format string is not available statically. It can, for instance, be coming from a configuration file. | ||
|
||
One common issue with Composite Formatting is that it is not immediately obvious how to expand named arguments. Fortunately, all the pieces of the puzzle are there. | ||
|
||
Just a little bit of F#: | ||
<code lang=fsharp> | ||
module Format = | ||
open System | ||
open System.Collections.Generic | ||
|
||
let private Split (s: string) = | ||
match s.IndexOf '|' with | ||
| -1 -> (s, "") | ||
| n -> (s.Substring(0, n), s.Substring(n + 1)) | ||
|
||
type Table<'T>(dict: IDictionary<string, 'T>) = | ||
new (pairs: seq<string * 'T>) = | ||
new Table<'T>(Map.ofSeq pairs) | ||
|
||
interface IFormattable with | ||
member this.ToString(format, _) = | ||
let (key, def) = Split format | ||
if dict.ContainsKey key then | ||
dict.[key].ToString() | ||
else | ||
def | ||
</code> | ||
Now we can use string keys (and default values) to expand on keywords within the format strings. This is handy: | ||
<code lang=fsharp> | ||
let fmt = "{0:schema|http}://{0:domain}{0:path}" | ||
System.String.Format(fmt, Format.Table ["domain", "example.com"]) | ||
System.String.Format(fmt, Format.Table ["domain", "example.com"; "path", "/products"]) | ||
System.String.Format(fmt, Format.Table ["schema", "https"; "domain", "example.com"]) | ||
</code> | ||
You can also pass <c>Dictionary</c> and <c>Map</c> objects to the <c>Format.Table</c> constructor. | ||
|
||
Even better, Composite Formatting is available not only in <c>String.Format</c> but also in other places such as <c>TextWriters</c>. | ||
|
||
As a functional programmer working with F#, I keep discovering basic .NET framework features. Even though this use of Composite Formatting must be trivial, I hope some of you will find it useful. |
49 changes: 49 additions & 0 deletions
49
user/anton.tayanovskyy/20091202-net-composite-formatting-with-keyword-expansion.md
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
--- | ||
title: ".NET Composite Formatting with Keyword Expansion" | ||
categories: "f#,dotnet" | ||
abstract: "" | ||
identity: "1458,74668" | ||
--- | ||
[Composite Formatting](https://msdn.microsoft.com/en-us/library/txafckwd%28VS.95%29.aspx) is a feature of .NET framework that comes handy even for F# programmers. Yes, `Printf`-style formatting generally is much nicer with F#, but there are situations where the format string is not available statically. It can, for instance, be coming from a configuration file. | ||
|
||
One common issue with Composite Formatting is that it is not immediately obvious how to expand named arguments. Fortunately, all the pieces of the puzzle are there. | ||
|
||
Just a little bit of F#: | ||
|
||
```fsharp | ||
module Format = | ||
open System | ||
open System.Collections.Generic | ||
let private Split (s: string) = | ||
match s.IndexOf '|' with | ||
| -1 -> (s, "") | ||
| n -> (s.Substring(0, n), s.Substring(n + 1)) | ||
type Table<'T>(dict: IDictionary<string, 'T>) = | ||
new (pairs: seq<string * 'T>) = | ||
new Table<'T>(Map.ofSeq pairs) | ||
interface IFormattable with | ||
member this.ToString(format, _) = | ||
let (key, def) = Split format | ||
if dict.ContainsKey key then | ||
dict.[key].ToString() | ||
else | ||
def | ||
``` | ||
|
||
Now we can use string keys (and default values) to expand on keywords within the format strings. This is handy: | ||
|
||
```fsharp | ||
let fmt = "{0:schema|http}://{0:domain}{0:path}" | ||
System.String.Format(fmt, Format.Table ["domain", "example.com"]) | ||
System.String.Format(fmt, Format.Table ["domain", "example.com"; "path", "/products"]) | ||
System.String.Format(fmt, Format.Table ["schema", "https"; "domain", "example.com"]) | ||
``` | ||
|
||
You can also pass `Dictionary` and `Map` objects to the `Format.Table` constructor. | ||
|
||
Even better, Composite Formatting is available not only in `String.Format` but also in other places such as `TextWriters`. | ||
|
||
As a functional programmer working with F#, I keep discovering basic .NET framework features. Even though this use of Composite Formatting must be trivial, I hope some of you will find it useful. |
42 changes: 42 additions & 0 deletions
42
user/anton.tayanovskyy/20091208-the-execution-speed-of-early-vs-late-binding-in-net.done
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
--- | ||
title: "The Execution Speed of Early vs Late Binding in .NET" | ||
categories: "websharper,f#" | ||
abstract: "" | ||
identity: "1456,74666" | ||
--- | ||
This little post documents one of my little experiments with F#, as I am educating myself on the .NET Framework fundamentals. | ||
|
||
The interesting issue is the execution speed of late vs early-bound code. Open the F# interactive and try this out. | ||
<code lang=fsharp> | ||
/// A dummy type. | ||
type T = | ||
/// This is the method we want to call. | ||
static member F x = x + 1 | ||
</code> | ||
Turn on timing in the Interactive: | ||
<code lang=fsharp> | ||
#time | ||
</code> | ||
First, let as assume we know the type and the method signature at compile time. This is early, static binding, and the result is fast: | ||
<code lang=fsharp> | ||
for i = 0 to 1000000 do | ||
T.F 1 | ||
|> ignore | ||
</code> | ||
Now suppose we do not know type <c>T</c> at compile time, but rather have a <c>System.Type</c> object to represent it. | ||
|
||
We could then use reflection to invoke the method, as below, but it is slow, several orders of magnitude slower in fact. | ||
<code lang=fsharp> | ||
let m = typeof<T>.GetMethod("F") | ||
for i = 0 to 1000000 do | ||
m.Invoke(null, [| box 1 |]) | ||
|> ignore | ||
</code> | ||
If we do not know the type T, but do know the method signature, we can do a lot better using delegates. This is fast: | ||
<code lang=fsharp> | ||
type F = delegate of int -> int | ||
let f = System.Delegate.CreateDelegate(typeof<F>, typeof<T>, "F") :?> F | ||
for i = 0 to 1000000 do | ||
f.Invoke 1 | ||
|> ignore | ||
</code> |
51 changes: 51 additions & 0 deletions
51
...ton.tayanovskyy/20091208-the-execution-speed-of-early-vs-late-binding-in-net.md
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
--- | ||
title: "The Execution Speed of Early vs Late Binding in .NET" | ||
categories: "f#,optimization,dotnet" | ||
abstract: "" | ||
identity: "1456,74666" | ||
--- | ||
This little post documents one of my little experiments with F#, as I am educating myself on the .NET Framework fundamentals. | ||
|
||
The interesting issue is the execution speed of late vs early-bound code. Open the F# interactive and try this out. | ||
|
||
```fsharp | ||
/// A dummy type. | ||
type T = | ||
/// This is the method we want to call. | ||
static member F x = x + 1 | ||
``` | ||
|
||
Turn on timing in the Interactive: | ||
|
||
```fsharp | ||
#time | ||
``` | ||
|
||
First, let as assume we know the type and the method signature at compile time. This is early, static binding, and the result is fast: | ||
|
||
```fsharp | ||
for i = 0 to 1000000 do | ||
T.F 1 | ||
|> ignore | ||
``` | ||
|
||
Now suppose we do not know type `T` at compile time, but rather have a `System.Type` object to represent it. | ||
|
||
We could then use reflection to invoke the method, as below, but it is slow, several orders of magnitude slower in fact. | ||
|
||
```fsharp | ||
let m = typeof<T>.GetMethod("F") | ||
for i = 0 to 1000000 do | ||
m.Invoke(null, [| box 1 |]) | ||
|> ignore | ||
``` | ||
|
||
If we do not know the type T, but do know the method signature, we can do a lot better using delegates. This is fast: | ||
|
||
```fsharp | ||
type F = delegate of int -> int | ||
let f = System.Delegate.CreateDelegate(typeof<F>, typeof<T>, "F") :?> F | ||
for i = 0 to 1000000 do | ||
f.Invoke 1 | ||
|> ignore | ||
``` |
64 changes: 64 additions & 0 deletions
64
user/anton.tayanovskyy/20091208-websharper-write-f-and-run-javascript.done
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
--- | ||
title: "WebSharper - Write F# and Run JavaScript" | ||
categories: "websharper,f#" | ||
abstract: "" | ||
identity: "1457,74667" | ||
--- | ||
It is an exciting time to be working for IntelliFactory. After quite a bit of hard work, we finally have a public beta release of the <a href="http://intellifactory.com/products/wsp/">WebSharper™ platform</a>. As we continue to work on it, I encourage you to download and play with the current beta release. | ||
|
||
WebSharper™ aims to change the way you think about web programming, no more and no less. The idea behind it is very simple. Instead of HTML + JavaScript + PHP/C#/Java code, you write F#, and let the compiler do its magic to get a working AJAX website. Alternatively, you develop a small component and expose it as an ASP.NET control, without having to rewrite your website from scratch. | ||
|
||
Let me start with a simple, indeed primitive, example. Let us take the the first problem from <a href="http://projecteuler.net/">Project Euler</a> (a great source of profitable amusement for many of us), solve it in F#, and run it in the browser. To play with it, download and install the beta release of the <a href="http://intellifactory.com/products/wsp/">WebSharper™ platform</a>, start a new WebSharper solution, and change the project code to the following: | ||
<code lang=fsharp> | ||
namespace WebSharperProject | ||
|
||
open IntelliFactory.WebSharper | ||
open IntelliFactory.WebSharper.Html | ||
|
||
[<assembly: WebSharperAssembly>] | ||
do () | ||
|
||
[<JavaScriptType>] | ||
module Math = | ||
|
||
/// Find the sum of all the multiples of 3 or 5 below 1000. | ||
[<JavaScript>] | ||
let ProjectEuler0001 = | ||
seq { 0 .. 999 } | ||
|> Seq.filter (fun x -> x % 3 = 0 || x % 5 = 0) | ||
|> Seq.sum | ||
|
||
[<JavaScriptType>] | ||
type Example() = | ||
inherit Web.Control() | ||
|
||
[<JavaScript>] | ||
override this.Body = | ||
let answer = Span [] | ||
Div [ | ||
Div [ | ||
Span ["The answer is: "] | ||
answer | ||
] | ||
Input [Type "Button"; Value "Calculate"] | ||
|> On Events.Click (fun _ -> | ||
answer.Text <- string Math.ProjectEuler0001) | ||
] | ||
</code> | ||
Then change the Default.aspx to reference the newly defined <c>Example</c> control: | ||
<code lang=xml> | ||
<%@ Page Language="C#" %> | ||
<html xmlns="http://www.w3.org/1999/xhtml"> | ||
<head runat="server"> | ||
<WebSharper:ScriptManager runat="server" /> | ||
</head> | ||
<body> | ||
<ws:Example runat="server" /> | ||
</body> | ||
</html> | ||
</code> | ||
This is a small example, but there is a lot to get excited about already. Take powerful F# type-checking, functional abstractions, embedded HTML combinators, or ASP.NET integration... | ||
|
||
Yes, Project Euler problems are not exactly representative of the tasks a typical web programmer faces. But I firmly believe that a platform that makes difficult things possible should make simlpe things simple. | ||
|
||
I will be blogging with more posts on WebSharper™ in the coming days. In the meanwhile, the impatient should definitely check out the <a href="http://intellifactory.com/products/wsp/Tutorial.aspx">Demos</a>. |
75 changes: 75 additions & 0 deletions
75
user/anton.tayanovskyy/20091208-websharper-write-f-and-run-javascript.md
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
--- | ||
title: "WebSharper - Write F# and Run JavaScript" | ||
categories: "f#,websharper,euler" | ||
abstract: "" | ||
identity: "1457,74667" | ||
--- | ||
It is an exciting time to be working for IntelliFactory. After quite a bit of hard work, we finally have a public beta release of the [WebSharper platform](https://websharper.com)[^1]. As we continue to work on it, I encourage you to download and play with the current beta release. | ||
|
||
WebSharper aims to change the way you think about web programming, no more and no less. The idea behind it is very simple. Instead of HTML + JavaScript + PHP/C#/Java code, you write F#, and let the compiler do its magic to get a working AJAX website. Alternatively, you develop a small component and expose it as an ASP.NET control, without having to rewrite your website from scratch. | ||
|
||
Let me start with a simple, indeed primitive, example. Let us take the the first problem from [Project Euler](http://projecteuler.net/) (a great source of profitable amusement for many of us), solve it in F#, and run it in the browser. To play with it, download and install the beta release of the [WebSharper platform](https://websharper.com)[^2], start a new WebSharper solution, and change the project code to the following: | ||
|
||
```fsharp | ||
namespace WebSharperProject | ||
open IntelliFactory.WebSharper | ||
open IntelliFactory.WebSharper.Html | ||
[<assembly: WebSharperAssembly>] | ||
do () | ||
[<JavaScriptType>] | ||
module Math = | ||
/// Find the sum of all the multiples of 3 or 5 below 1000. | ||
[<JavaScript>] | ||
let ProjectEuler0001 = | ||
seq { 0 .. 999 } | ||
|> Seq.filter (fun x -> x % 3 = 0 || x % 5 = 0) | ||
|> Seq.sum | ||
[<JavaScriptType>] | ||
type Example() = | ||
inherit Web.Control() | ||
[<JavaScript>] | ||
override this.Body = | ||
let answer = Span [] | ||
Div [ | ||
Div [ | ||
Span ["The answer is: "] | ||
answer | ||
] | ||
Input [Type "Button"; Value "Calculate"] | ||
|> On Events.Click (fun _ -> | ||
answer.Text <- string Math.ProjectEuler0001) | ||
] | ||
``` | ||
|
||
Then change `Default.aspx` to reference the newly defined `Example` control: | ||
|
||
```xml | ||
<%@ Page Language="C#" %> | ||
<html xmlns="http://www.w3.org/1999/xhtml"> | ||
<head runat="server"> | ||
<WebSharper:ScriptManager runat="server" /> | ||
</head> | ||
<body> | ||
<ws:Example runat="server" /> | ||
</body> | ||
</html> | ||
``` | ||
|
||
This is a small example, but there is a lot to get excited about already. Take powerful F# type-checking, functional abstractions, embedded HTML combinators, or ASP.NET integration... | ||
|
||
Yes, Project Euler problems are not exactly representative of the tasks a typical web programmer faces. But I firmly believe that a platform that makes difficult things possible should make simlpe things simple. | ||
|
||
I will be blogging with more posts on WebSharper in the coming days. In the meanwhile, the impatient should definitely check out the [Demos](https://try.websharper.com)[^3]. | ||
|
||
|
||
[^1]: This link has been updated to point to the WebSharper home page. | ||
|
||
[^2]: This link has been updated to point to the WebSharper home page. | ||
|
||
[^3]: This link has been updated to point to the Try WebSharper home page. |
Oops, something went wrong.