Keith Pinson
I am a software developer who is getting into speaking.
let function input = output
var trimmed = "";
if (toTrim != null)
{
trimmed = toTrim.Trim();
}
return trimmed;
var x = 2;
return x;
if toTrim = null
then ""
else toTrim.Trim()
class Cell {
public bool IsAlive { get; set; }
public Cell NextState(int livingNeighbors)
{
if ((IsAlive && livingNeighbors == 2) ||
livingNeighbors == 3)
{
return new Cell { IsAlive = true };
}
return new Cell { IsAlive = false };
}
}
type ParseResult =
| Success of Expression
| Incomplete
| Failure of string
type Option<'a> = None | Some of 'a
match parseResult with
| Succeeded expr ->
print (evaluate expr)
| Failed message ->
printError message
| Incomplete ->
evalAndPrint (readAnotherLine())
type Cell = DeadCell | LivingCell
let nextStateOf cell livingNeighbors =
match cell, livingNeighbors with
| LivingCell, 2 -> LivingCell
| _, 3 -> LivingCell
| _ -> DeadCell
public bool ShouldOfferToGiftWrap()
{
foreach (var item in Items)
{
if (item.IsFromWishlist)
{
return true;
}
}
return false;
}
let shouldOfferToWrapGift order =
List.exists (fun i -> i.IsFromWishlist) order.Items
let selectDaleks aggregate episode =
match episode with
| 0, _ -> aggregate
| 1, episodeName ->
sprintf "%s\n1 dalek in %s" aggregate episodeName
| n, episodeName ->
sprintf "%s\n%i daleks in %s" aggregate n episodeName
List.fold selectDaleks "Dalek episodes:" episodes |> printf "%s"
seq { ... }
type Pizza = {
Name : string
Ingredients : string list
IsDeluxe : bool
}
let pepperoni = {
Name = "Pepperoni"
Ingredients = ["Pepperoni"; "Cheese"]
IsDeluxe = false
}
{ pepperoni with Ingredients = "mushrooms" :: pizza.Ingredients }
List.filter hasDaleks doctorWhoEpisodes
|> List.map (fun episode -> episode.Name)
|> printf "Favorite episodes: %A"
onlyDalekEpisodes doctorWhoEpisodes
|> List.map (fun episode -> episode.Name)
|> printf "Favorite episodes: %A"
let plusThree x = x + 3
let timesTwo x = x * 2
let plusThreeTimesTwo =
plusThree >> timesTwo
// Or just
let plusThreeTimesTwo =
(+) 3 >> (*) 2
let hasDaleks episode =
...
let onlyDalekEpisodes =
List.filter hasDaleks
(* Same as
* let onlyDalekEpisodes episodes =
* List.filter hasDaleks episodes
* *)
onlyDalekEpisodes episodes
onlyDalekEpisodes doctorWhoEpisodes
|> List.map (fun episode -> episode.Name)
|> printf "Favorite episodes: %A"
lazyLoadFromDisk()
|> Seq.filter ...
|> Seq.map ...
|> Seq.reduce ...
|> writeToDisk
By Keith Pinson
The practical value of thinking functionally and expressing it in F#
I am a software developer who is getting into speaking.