https://github.com/ken-okabe/vscode-lambda-for-fun-fsharp
Write Haskell style lambda expression in F#, `\` instead of `fun `
https://github.com/ken-okabe/vscode-lambda-for-fun-fsharp
dotnet dotnet-framework fsharp functional-programming haskell lambda lambda-calculus lambda-expressions lambda-functions visual-studio-code
Last synced: 30 days ago
JSON representation
Write Haskell style lambda expression in F#, `\` instead of `fun `
- Host: GitHub
- URL: https://github.com/ken-okabe/vscode-lambda-for-fun-fsharp
- Owner: ken-okabe
- License: mit
- Created: 2022-08-24T20:49:55.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2023-01-11T21:43:14.000Z (over 2 years ago)
- Last Synced: 2025-01-31T04:12:44.329Z (3 months ago)
- Topics: dotnet, dotnet-framework, fsharp, functional-programming, haskell, lambda, lambda-calculus, lambda-expressions, lambda-functions, visual-studio-code
- Language: TypeScript
- Homepage: https://marketplace.visualstudio.com/items?itemName=KenOkabe.lambda-for-fun-fsharp
- Size: 86.9 KB
- Stars: 2
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# [Deprecated]Lambda for fun F#

**Write Haskell style Lambda Expression in F#:**
`\x` instead of `fun x`#### Before

#### After
#### Even in Comments

## Deprecated
Please use the below instead
---
> ## [Conceal for VSCode](https://marketplace.visualstudio.com/items?itemName=BRBoer.vsc-conceal)
>Conceal makes *visual* substitutions to your source code, e.g. displaying `fun` as `λ`, while never touching your code.
>This feature is inspired by [prettify-symbols-mode for Emacs](https://www.emacswiki.org/emacs/PrettySymbol) and is the unofficial successor of [vsc-prettify-symbols-mode](https://github.com/siegebell/vsc-prettify-symbols-mode).
Configuration for `fun ` to `\`
```json
"conceal.substitutions": [{
"language": "fsharp",
"substitutions": [
{
"ugly": "fun ",
"pretty": "\\",
"pre": "\\b"
}
]
}]
```### `[Alt] + \` will type the virtual `\`
`keybinding.json` as below:
```json
[
{
"key": "alt+\\",
"command": "editor.action.insertSnippet",
"when": "editorFocus && editorLangId == 'fsharp'",
"args": {
"snippet": "fun $1"
}
}
]
```----
--------
## Features and benefits
✓ [Proven syntax in Haskell for Lambda Expressions](https://wiki.haskell.org/Monad_laws) that corresponds directly to [F# legacy `fun_` syntax](https://docs.microsoft.com/en-us/dotnet/fsharp/language-reference/functions/lambda-expressions-the-fun-keyword)✓ You can enjoy the concise Haskell style Lambda Expression before the official implementation without messing your code
✓ The F# code only appears so virtually on the VSCode editor with this extension, Nothing changes
✓ Simple design, No command, Just do the single task, Works out-of-the-box!
## How is this possible?
CSS with VSCode editor decorator API. VSCode is built on the Web Technology!
[microsoft/vscode-extension-samples/decorator-sample/](https://github.com/microsoft/vscode-extension-samples/tree/main/decorator-sample)
Although I observed the official sample code is not written smartly in FunctionalProgramming style, this extension is derived from it with minimal code modifications.
The majority of the trick comes from CSS modification.
Inspired by the great work of **Inline fold** extension @[Marketplace](https://marketplace.visualstudio.com/items?itemName=moalamri.inline-fold)/[GitHub Repo](https://github.com/moalamri/vscode-inline-fold), here is the core implementation of the decorator CSS.
---
```typescript
// create a decorator type that we use to decorate the matched keyword
decorationType = vscode.window.createTextEditorDecorationType(
{
before: {
contentText: "\\",
color: config.color,
},
textDecoration: "none; display: none;",
}
);
```So, this CSS works as below:
STEP 1: Add `\` (virtual not real text, like the other type hints) `before` the `fun_`

STEP 2: Hide the `fun_` (real text) virtually

---
I also tried `after` scenario instead of `before`, but it doesn't work well at all.
## Known Issues
The `fun_` is hidden by CSS, but actually remains to exist as the real element on the VSCode text editor, and the same applies to the real cursor move. Therefore, the cursor move around `\` make you feel against your standard editing experience.
## How to delete such a virtual existence??
Just try to delete, and it works!

When you try to delete the virtual `\`, you will actually delete the real SPACE at the end of `fun_`. This will collapse the regex match for `fun_` including the last space, so `funx` will emerge on the surface of the text editor.
## How to type the virtual `\` ?
`[Alt] + \` will type the virtual `\` as the default setting as below:
```json
"keybindings": [
{
"key": "alt+\\",
"command": "editor.action.insertSnippet",
"when": "editorFocus && editorLangId == 'fsharp'",
"args": {
"snippet": "fun $1"
}
}
]
```If you want to change the keybinding, you should be able to [access and manage VSCode keybindings](https://code.visualstudio.com/docs/getstarted/keybindings).
## How to Get Started?
Just Install then it should work out-of-the-box!
This extension will be activated on every event so supposedly, reboot of VSCode or reopening F# code editor is not required.
After the activation of this extension, a couple of TextEditor events are monitored and if the document is F# document (the language id is `fsharp`), after a certain delay (100 milliseconds as default), it will trigger `updateDecorations` that includes regex search.
This delay mechanism is directly derived as it is in the Microsoft official sample code, and I have not touched.
```typescript
// when supported languages, some smart delay update
// the inner code is derived from MS official sample
function triggerUpdateDecorations(throttle: boolean, editor: vscode.TextEditor) {editor.document.languageId === "fsharp"
? (() => {
if (timeout) {
clearTimeout(timeout);
timeout = undefined;
}
if (throttle) {
timeout = setTimeout(updateDecorations, delay);
} else {
updateDecorations();
}
})()
: (() => { })();}
```Probably it's implemented as so to avoid jamming events by user key strokes in a short period of the time.
## Requirements
VisualStudioCode 1.70.0 or higher
## Install
Just use the GUI or CUI of VSCode that you know already.
## Extension Settings
This extension is designed to run out-of-the-box for your F# Code!
The 3 settings are available:
#### Decoration color of lambda backslash(`\`)
`lambda-for-fun-fsharp.color` : `#68D7AC`
#### Delay milliseconds of updateDecorations
`lambda-for-fun-fsharp.delay` : `100`
#### Regex to identify `fun_`
`lambda-for-fun-fsharp.regex` : *empty*
## Regex to match `fun_` keyword
The value for `lambda-for-fun-fsharp.regex` is *empty* as default, and a hardcoded regex as seen in the URL below will be used :
https://regex101.com/r/U9IeWI/1
When you want to try a better regex, after testing at the site, Copy it manually except the head `/` and tail `\g`, then Paste at the form of `lambda-for-fun-fsharp.regex`.

## Your contribution is highly appreciated
If you have some idea for improvement, better regex or any other issues, please join
https://github.com/stken2050/vscode-lambda-for-fun-fsharp/issues
---
**Enjoy F# Coding!**