• Stars
    star
    1
  • Language
    F#
  • Created over 3 years ago
  • Updated over 3 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

F# hints

F# CheatSheet

# Create new project
dotnet new console -n test1 -lang f#

Tweets

Samples

Codes

// Functions
let linear x = 2.0 * x
let quadratic x = x ** 2

// read a line on console
open System
let line = Console.ReadLine()
printfn "Read line %s" line

// Defining a list
let lista = [
    2
    3 ]

// List just par numbers from a descrecent list
[9 .. -1 .. 1] |> List.filter (fun x -> x % 2 = 0)

// Calling an API using the F# Data Http utility:
// Source: https://stackoverflow.com/questions/62408621/f-with-http-fs-not-able-to-execute-graphql-apis
Http.RequestString
  ( "https://swapi.graph.cool", 
    httpMethod="POST", headers=[ HttpRequestHeaders.ContentType("application/json") ],
    body=TextRequest("{\"query\": \"{ allFilms { title } }\"}") 

printfn

// hello world
open System
[<EntryPoint>]
let main argv =
    let square x = x * x
    let squared = List.map square [1;2;3;5;7]
    printfn "%A" squared
    0 // return an integer exit code

Array.map (fun x -> -x) [|0..9|];;
// val it : int [] = [|0; -1; -2; -3; -4; -5; -6; -7; -8; -9|]

List.map (fun x -> -x) [0..9];;
// val it : int list = [0; -1; -2; -3; -4; -5; -6; -7; -8; -9]

Seq.map (fun x -> -x)  { 0..9 };;
// val it : seq<int> = seq [0; -1; -2; -3; ...]
Format Type
%s string
%i integer
%u unsigned integer
%f float
%b boolean
%O other objects
%o octal
%x lowercase hex
%X uppercase hex
%A native F# types

functions

open System
let ``print emoji  ๐Ÿฃ`` =
    printfn "๐Ÿฃ"
``print emoji  ๐Ÿฃ``

F# Types with .NET 5

Source

Just as a quick update, various things do work.

This example now works in .NET 5:

open System
open System.Text.Json


[<EntryPoint>]
let main _argv =

    let m1 = Map.empty<string, string> |> Map.add "answer" "42"
    let json1 = JsonSerializer.Serialize(m1)
    printfn "JSON1 %s" json1
    
    let m2 = Map.empty<string, Version> |> Map.add "version" (Version("10.0.1.1"))
    let json2 = JsonSerializer.Serialize(m2)
    printfn "JSON2 %s" json2

    0

F# records and anonymous records now appear to work (nested records example):

open System
open System.Text.Json

type Person = { Name: string; Age: int }
type Gaggle = { People: Person list; Name: string }

[<EntryPoint>]
let main _argv =

    let r1 =
        {
            People =
                [ { Name = "Phillip"; Age = Int32.MaxValue }
                  { Name = "Ryan Nowak"; Age = Int32.MinValue } ]
            Name = "Meme team"
        }

    let r2 =
        {|
            r1 with
                FavoriteMusic = "Dan Roth's Banjo Bonanza"
        |}

    printfn $"{JsonSerializer.Serialize r1}"
    printfn $"{JsonSerializer.Serialize r2}"

    0

But DUs are still missing. FSharp.SystemTextJson is still the way to serialize F# union types.

C# Interop

// ResizeArray is the name of the #CSharp generic list class in #FSharp, so you can do:
let csList = ResizeArray  [| rec01; rec02; rec03 |] 

Using use

// Fonte: https://twitter.com/LucasTeles42/status/1317945856641978375
open System

let main =
  use recurso = {
    new IDisposable with
      member this.Dispose() =
        printfn "liberando recurso..."
  }
  printfn "acabou..."

Using Feliz

List Comprehension

// Source https://twitter.com/LucasTeles42/status/1335981686794936320
> let items x = [
-     1
-     2
-     if x then 3
-
-     let n = 10
-     yield! [4..n]
-
-     n + 1
-
-     for numero = n + 2 to n * 2 do
-         numero
- ];;
val items : x:bool -> int list

> printfn "%A" (items true);;
[1; 2; 3; 4; 5; 6; 7; 8; 9; 10; 11; 12; 13; 14; 15; 16; 17; 18; 19; 20]
val it : unit = ()

Sync/Async

let work = async { do printfn "Hi" }

work
|> Async.RunSynchronously

FSI (F# Repl)

Started with:

dotnet fsi

Options:

  • #help - Exibe informaรงรตes sobre as diretivas disponรญveis.
  • #I - Especifica um caminho de pesquisa de assembly entre aspas.
  • #load - Lรช um arquivo de origem, compila e o executa.
  • #quit - Finaliza uma sessรฃo do F# interativo.
  • #r - Faz referรชncia a um assembly.
  • #time ["on"|"off"] - Por si sรณ, #time alterna se deve exibir informaรงรตes sobre o desempenho. Quando habilitado, o F# interativo mede informaรงรตes em tempo real, em tempo de CPU e de coleta de lixo de cada seรงรฃo do cรณdigo que รฉ interpretado e executado.

References:

More Repositories

1

neoline.vim

Status Line for Neovim focused on beauty and performance โœ…๐Ÿ–ค๐Ÿ’™๐Ÿ’›
Lua
189
star
2

fsharp-cheatsheet

An updated cheat sheet for F# ๐Ÿ”ท๐Ÿ–ค๐Ÿ’™๐Ÿ’›
183
star
3

vim-emoji-icon-theme

Emoji/Unicode Icons Theme for Vim and Neovim with support for 40+ plugins and 380+ filetypes ๐ŸŽจ๐Ÿ–ค๐Ÿ’™๐Ÿ’›
Vim Script
77
star
4

vim-matchit

vim-matchit ๐Ÿ–‡๐Ÿ–ค๐Ÿ’™๐Ÿ’›
Vim Script
54
star
5

image_preview.nvim

Image Preview for Neovim ๐Ÿ–ผ
Lua
44
star
6

fsharp_vids

Video collection about F# ๐Ÿ”ท๐ŸŽž๏ธ
38
star
7

neofsharp.vim

Basic F# support for (Neo)Vim ๐Ÿ”ท
Vim Script
34
star
8

awesome-fsharp-project-templates

Awesome F# Project Templates ๐Ÿ”ท๐Ÿ–ค๐Ÿ’™๐Ÿ’›
19
star
9

neovcs.vim

VCS support for Neovim ๐ŸŒฑ
Vim Script
18
star
10

hotchocolate_fsharp_sample

GraphQL server using .NET 6, F# and Hot Chocolate ๐Ÿซ
18
star
11

vim-hackernews

Hacker News plugin for Vim ๐Ÿ“ฐ๐Ÿ–ค๐Ÿ’™๐Ÿ’›
Python
17
star
12

vim-pomodoro

๐Ÿ… Vim plugin for the Pomodoro time management technique
Vim Script
14
star
13

falco_graphql_sample

Falco GraphQL Sample ๐Ÿฃ๐Ÿ–ค๐Ÿ’™๐Ÿ’›
F#
14
star
14

awesome-fable-react-native

An awesome list about to use Fable with ReactNative ๐Ÿงšโ€โ™€๏ธ
11
star
15

eclipse-gotofile-plugin-kai

๐ŸŒ• Modified version from Muermann's Goto File plugin
Java
11
star
16

vim-grimmjow

๐ŸŒด Vim theme
Vim Script
9
star
17

suave_graphql_sample

Suave GraphQL Sample
F#
8
star
18

awesome-bugs

Awesome Bugs ๐Ÿ›
6
star
19

Vim-Autoclose

โœ’๏ธ Inserts matching bracket, paren, brace or quote
Vim Script
6
star
20

lightlang

General purpose programming language made for peace ๐Ÿ–ค๐Ÿ’™๐Ÿ’›
5
star
21

awesome-fsharp-pt

Uma lista com conteรบdos sobre F# em Lรญngua Portuguesa
4
star
22

vim-tetris

Tetris para Vim
Vim Script
3
star
23

nerdtree-terminal

nerdtree plugin terminal support
Vim Script
2
star
24

dbunit

๐Ÿ DBUnit
Java
2
star
25

Sublime-Theme-Black-Sidebar-Icons

๐Ÿซ A black theme for sidebar from Sublime with icons.
2
star
26

lsp_installer

Lua
1
star
27

adelarsq.github.io

Adelar's Home Page
1
star
28

jms-demo-play-2.1

๐Ÿ“ง jms-demo with play 2.1
Java
1
star
29

nerdtree-fzf

NERDTree Plugin to use with FZF
Vim Script
1
star
30

nerdtree-far

Nerdtree Plugin to use with Far
Vim Script
1
star
31

showmarks.nvim

Show marks on current buffer
Fennel
1
star
32

fsharp_samples

F# Samples
F#
1
star
33

Vim-Tetr1s

๐Ÿ“Š Tetris Game for Vim
1
star
34

BufJump

๐Ÿš€ Mapping to manage jump between buffer
Vim Script
1
star
35

learn_fsharp_tweets

Learn F# Tweets ๐Ÿ”ท
1
star
36

Snake.vim

๐Ÿ Snake Game for Vim
Vim Script
1
star
37

ignored_files.vim

Plugin to ignore filetypes and buffers for a number of plugins
Vim Script
1
star