added doi
This commit is contained in:
199
dist/Coding/Haskell/Lenses.html
vendored
199
dist/Coding/Haskell/Lenses.html
vendored
@ -2,7 +2,7 @@
|
||||
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en"><head>
|
||||
|
||||
<meta charset="utf-8">
|
||||
<meta name="generator" content="quarto-1.7.23">
|
||||
<meta name="generator" content="quarto-1.8.17">
|
||||
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes">
|
||||
|
||||
@ -68,17 +68,19 @@ pre > code.sourceCode > span > a:first-child::before { text-decoration: underlin
|
||||
<meta name="quarto:offset" content="../../">
|
||||
<script src="../../site_libs/quarto-html/quarto.js" type="module"></script>
|
||||
<script src="../../site_libs/quarto-html/tabsets/tabsets.js" type="module"></script>
|
||||
<script src="../../site_libs/quarto-html/axe/axe-check.js" type="module"></script>
|
||||
<script src="../../site_libs/quarto-html/popper.min.js"></script>
|
||||
<script src="../../site_libs/quarto-html/tippy.umd.min.js"></script>
|
||||
<script src="../../site_libs/quarto-html/anchor.min.js"></script>
|
||||
<link href="../../site_libs/quarto-html/tippy.css" rel="stylesheet">
|
||||
<link href="../../site_libs/quarto-html/quarto-syntax-highlighting-dark-2c84ecb840a13f4c7993f9e5648f0c14.css" rel="stylesheet" class="quarto-color-scheme quarto-color-alternate" id="quarto-text-highlighting-styles">
|
||||
<link href="../../site_libs/quarto-html/quarto-syntax-highlighting-6cf5824034cebd0380a5b9c74c43f006.css" rel="stylesheet" class="quarto-color-scheme" id="quarto-text-highlighting-styles">
|
||||
<link href="../../site_libs/quarto-html/quarto-syntax-highlighting-ff1c8a91a872873ab9706226229f9e30.css" rel="stylesheet" class="quarto-color-scheme" id="quarto-text-highlighting-styles">
|
||||
<link href="../../site_libs/quarto-html/quarto-syntax-highlighting-dark-04b38c9dba5d7c0f1fe2b621b39e7e62.css" rel="stylesheet" class="quarto-color-scheme quarto-color-alternate" id="quarto-text-highlighting-styles">
|
||||
<link href="../../site_libs/quarto-html/quarto-syntax-highlighting-ff1c8a91a872873ab9706226229f9e30.css" rel="stylesheet" class="quarto-color-scheme-extra" id="quarto-text-highlighting-styles">
|
||||
<script src="../../site_libs/bootstrap/bootstrap.min.js"></script>
|
||||
<link href="../../site_libs/bootstrap/bootstrap-icons.css" rel="stylesheet">
|
||||
<link href="../../site_libs/bootstrap/bootstrap-ec71cb1e120c0dd41819aca960e74e38.min.css" rel="stylesheet" append-hash="true" class="quarto-color-scheme" id="quarto-bootstrap" data-mode="light">
|
||||
<link href="../../site_libs/bootstrap/bootstrap-dark-716ed94a23403968eaa6fe981e0cbf91.min.css" rel="stylesheet" append-hash="true" class="quarto-color-scheme quarto-color-alternate" id="quarto-bootstrap" data-mode="dark">
|
||||
<link href="../../site_libs/bootstrap/bootstrap-ec71cb1e120c0dd41819aca960e74e38.min.css" rel="stylesheet" append-hash="true" class="quarto-color-scheme-extra" id="quarto-bootstrap" data-mode="light">
|
||||
<link href="../../site_libs/bootstrap/bootstrap-9b56b6674ace31b1957ea5f5ef42286f.min.css" rel="stylesheet" append-hash="true" class="quarto-color-scheme" id="quarto-bootstrap" data-mode="light">
|
||||
<link href="../../site_libs/bootstrap/bootstrap-dark-3c8015a3c2181875b3e8a350495bd83c.min.css" rel="stylesheet" append-hash="true" class="quarto-color-scheme quarto-color-alternate" id="quarto-bootstrap" data-mode="dark">
|
||||
<link href="../../site_libs/bootstrap/bootstrap-9b56b6674ace31b1957ea5f5ef42286f.min.css" rel="stylesheet" append-hash="true" class="quarto-color-scheme-extra" id="quarto-bootstrap" data-mode="light">
|
||||
<script id="quarto-search-options" type="application/json">{
|
||||
"location": "navbar",
|
||||
"copy-button": false,
|
||||
@ -131,7 +133,7 @@ pre > code.sourceCode > span > a:first-child::before { text-decoration: underlin
|
||||
toggleBodyColorMode(bsSheetEl);
|
||||
}
|
||||
}
|
||||
window.setColorSchemeToggle = (alternate) => {
|
||||
const setColorSchemeToggle = (alternate) => {
|
||||
const toggles = window.document.querySelectorAll('.quarto-color-scheme-toggle');
|
||||
for (let i=0; i < toggles.length; i++) {
|
||||
const toggle = toggles[i];
|
||||
@ -164,7 +166,7 @@ pre > code.sourceCode > span > a:first-child::before { text-decoration: underlin
|
||||
}
|
||||
manageTransitions('#quarto-margin-sidebar .nav-link', true);
|
||||
// Switch the toggles
|
||||
window.setColorSchemeToggle(alternate)
|
||||
setColorSchemeToggle(alternate)
|
||||
// Hack to workaround the fact that safari doesn't
|
||||
// properly recolor the scrollbar when toggling (#1455)
|
||||
if (navigator.userAgent.indexOf('Safari') > 0 && navigator.userAgent.indexOf('Chrome') == -1) {
|
||||
@ -204,7 +206,7 @@ pre > code.sourceCode > span > a:first-child::before { text-decoration: underlin
|
||||
const isFileUrl = () => {
|
||||
return window.location.protocol === 'file:';
|
||||
}
|
||||
window.hasAlternateSentinel = () => {
|
||||
const hasAlternateSentinel = () => {
|
||||
let styleSentinel = getColorSchemeSentinel();
|
||||
if (styleSentinel !== null) {
|
||||
return styleSentinel === "alternate";
|
||||
@ -232,7 +234,7 @@ pre > code.sourceCode > span > a:first-child::before { text-decoration: underlin
|
||||
const baseTheme = document.querySelector('#giscus-base-theme')?.value ?? 'light';
|
||||
const alternateTheme = document.querySelector('#giscus-alt-theme')?.value ?? 'dark';
|
||||
let newTheme = '';
|
||||
if(darkModeDefault) {
|
||||
if(authorPrefersDark) {
|
||||
newTheme = isAlternate ? baseTheme : alternateTheme;
|
||||
} else {
|
||||
newTheme = isAlternate ? alternateTheme : baseTheme;
|
||||
@ -255,17 +257,20 @@ pre > code.sourceCode > span > a:first-child::before { text-decoration: underlin
|
||||
changeGiscusTheme();
|
||||
}
|
||||
};
|
||||
const authorPrefersDark = false;
|
||||
const queryPrefersDark = window.matchMedia('(prefers-color-scheme: dark)');
|
||||
const darkModeDefault = queryPrefersDark.matches;
|
||||
document.querySelector('link.quarto-color-scheme-extra').rel = 'disabled-stylesheet';
|
||||
document.querySelector('link#quarto-text-highlighting-styles.quarto-color-scheme-extra').rel = 'disabled-stylesheet';
|
||||
document.querySelector('link#quarto-bootstrap.quarto-color-scheme-extra').rel = 'disabled-stylesheet';
|
||||
let localAlternateSentinel = darkModeDefault ? 'alternate' : 'default';
|
||||
// Dark / light mode switch
|
||||
window.quartoToggleColorScheme = () => {
|
||||
// Read the current dark / light value
|
||||
let toAlternate = !window.hasAlternateSentinel();
|
||||
let toAlternate = !hasAlternateSentinel();
|
||||
toggleColorMode(toAlternate);
|
||||
setStyleSentinel(toAlternate);
|
||||
toggleGiscusIfUsed(toAlternate, darkModeDefault);
|
||||
window.dispatchEvent(new Event('resize'));
|
||||
};
|
||||
queryPrefersDark.addEventListener("change", e => {
|
||||
if(window.localStorage.getItem("quarto-color-scheme") !== null)
|
||||
@ -276,7 +281,7 @@ pre > code.sourceCode > span > a:first-child::before { text-decoration: underlin
|
||||
toggleGiscusIfUsed(alternate, darkModeDefault);
|
||||
});
|
||||
// Switch to dark mode if need be
|
||||
if (window.hasAlternateSentinel()) {
|
||||
if (hasAlternateSentinel()) {
|
||||
toggleColorMode(true);
|
||||
} else {
|
||||
toggleColorMode(false);
|
||||
@ -288,6 +293,8 @@ pre > code.sourceCode > span > a:first-child::before { text-decoration: underlin
|
||||
<nav class="navbar navbar-expand-lg " data-bs-theme="dark">
|
||||
<div class="navbar-container container-fluid">
|
||||
<div class="navbar-brand-container mx-auto">
|
||||
<a href="../../index.html" class="navbar-brand navbar-brand-logo">
|
||||
</a>
|
||||
<a class="navbar-brand" href="../../index.html">
|
||||
<span class="navbar-title">Nicole Dresselhaus</span>
|
||||
</a>
|
||||
@ -299,12 +306,12 @@ pre > code.sourceCode > span > a:first-child::before { text-decoration: underlin
|
||||
<div class="collapse navbar-collapse" id="navbarCollapse">
|
||||
<ul class="navbar-nav navbar-nav-scroll me-auto">
|
||||
<li class="nav-item">
|
||||
<a class="nav-link" href="../../index.html"> <i class="bi bi-house" role="img">
|
||||
<a class="nav-link active" href="../../index.html" aria-current="page"> <i class="bi bi-house" role="img">
|
||||
</i>
|
||||
<span class="menu-text">Home</span></a>
|
||||
</li>
|
||||
<li class="nav-item">
|
||||
<a class="nav-link active" href="../../About/index.html" aria-current="page"> <i class="bi bi-file-person" role="img">
|
||||
<a class="nav-link" href="../../About/index.html"> <i class="bi bi-file-person" role="img">
|
||||
</i>
|
||||
<span class="menu-text">About</span></a>
|
||||
</li>
|
||||
@ -342,6 +349,10 @@ pre > code.sourceCode > span > a:first-child::before { text-decoration: underlin
|
||||
<div id="quarto-content" class="quarto-container page-columns page-rows-contents page-layout-article page-navbar">
|
||||
<!-- sidebar -->
|
||||
<nav id="quarto-sidebar" class="sidebar collapse collapse-horizontal quarto-sidebar-collapse-item sidebar-navigation docked overflow-auto">
|
||||
<div class="pt-lg-2 mt-2 text-left sidebar-header">
|
||||
<a href="../../index.html" class="sidebar-logo-link">
|
||||
</a>
|
||||
</div>
|
||||
<div class="sidebar-menu-container">
|
||||
<ul class="list-unstyled mt-1">
|
||||
<li class="sidebar-item sidebar-item-section">
|
||||
@ -385,12 +396,6 @@ pre > code.sourceCode > span > a:first-child::before { text-decoration: underlin
|
||||
<a href="../../Writing/Obsidian-RAG.html" class="sidebar-item-text sidebar-link">
|
||||
<span class="menu-text">RAG für eine Obsidian-Wissensdatenbank: Technische Ansätze</span></a>
|
||||
</div>
|
||||
</li>
|
||||
<li class="sidebar-item">
|
||||
<div class="sidebar-item-container">
|
||||
<a href="../../Obsidian-Articles/Artikelbewertung durch LLM.html" class="sidebar-item-text sidebar-link">
|
||||
<span class="menu-text">Artikelbewertung durch ein Large Language Model (LLM) - Ein Beispiel aus der Praxis</span></a>
|
||||
</div>
|
||||
</li>
|
||||
</ul>
|
||||
</li>
|
||||
@ -691,7 +696,7 @@ kann. Also einfachen Datenstruktur kann man einen Record mit der entsprechenden<
|
||||
Syntax nehmen.</p>
|
||||
<section id="beispiel" class="level3">
|
||||
<h3 class="anchored" data-anchor-id="beispiel">Beispiel</h3>
|
||||
<div class="sourceCode" id="cb1"><pre class="sourceCode haskell code-with-copy"><code class="sourceCode haskell"><span id="cb1-1"><a href="#cb1-1" aria-hidden="true" tabindex="-1"></a><span class="kw">data</span> <span class="dt">Person</span> <span class="ot">=</span> <span class="dt">P</span> {<span class="ot"> name ::</span> <span class="dt">String</span></span>
|
||||
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb1"><pre class="sourceCode haskell code-with-copy"><code class="sourceCode haskell"><span id="cb1-1"><a href="#cb1-1" aria-hidden="true" tabindex="-1"></a><span class="kw">data</span> <span class="dt">Person</span> <span class="ot">=</span> <span class="dt">P</span> {<span class="ot"> name ::</span> <span class="dt">String</span></span>
|
||||
<span id="cb1-2"><a href="#cb1-2" aria-hidden="true" tabindex="-1"></a> ,<span class="ot"> addr ::</span> <span class="dt">Address</span></span>
|
||||
<span id="cb1-3"><a href="#cb1-3" aria-hidden="true" tabindex="-1"></a> ,<span class="ot"> salary ::</span> <span class="dt">Int</span> }</span>
|
||||
<span id="cb1-4"><a href="#cb1-4" aria-hidden="true" tabindex="-1"></a><span class="kw">data</span> <span class="dt">Address</span> <span class="ot">=</span> <span class="dt">A</span> {<span class="ot"> road ::</span> <span class="dt">String</span></span>
|
||||
@ -705,7 +710,7 @@ Syntax nehmen.</p>
|
||||
<span id="cb1-12"><a href="#cb1-12" aria-hidden="true" tabindex="-1"></a><span class="ot"> setPostcode ::</span> <span class="dt">String</span> <span class="ot">-></span> <span class="dt">Person</span> <span class="ot">-></span> <span class="dt">Person</span></span>
|
||||
<span id="cb1-13"><a href="#cb1-13" aria-hidden="true" tabindex="-1"></a> setPostcode pc p</span>
|
||||
<span id="cb1-14"><a href="#cb1-14" aria-hidden="true" tabindex="-1"></a> <span class="ot">=</span> p { addr <span class="ot">=</span> addr p { postcode <span class="ot">=</span> pc } }</span>
|
||||
<span id="cb1-15"><a href="#cb1-15" aria-hidden="true" tabindex="-1"></a> <span class="co">-- update of a record inside a record</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||||
<span id="cb1-15"><a href="#cb1-15" aria-hidden="true" tabindex="-1"></a> <span class="co">-- update of a record inside a record</span></span></code></pre></div><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></div>
|
||||
</section>
|
||||
<section id="problem" class="level3">
|
||||
<h3 class="anchored" data-anchor-id="problem">Problem</h3>
|
||||
@ -720,7 +725,7 @@ interessiert sind</li>
|
||||
</section>
|
||||
<section id="was-wir-gern-hätten" class="level3">
|
||||
<h3 class="anchored" data-anchor-id="was-wir-gern-hätten">Was wir gern hätten</h3>
|
||||
<div class="sourceCode" id="cb2"><pre class="sourceCode haskell code-with-copy"><code class="sourceCode haskell"><span id="cb2-1"><a href="#cb2-1" aria-hidden="true" tabindex="-1"></a><span class="kw">data</span> <span class="dt">Person</span> <span class="ot">=</span> <span class="dt">P</span> {<span class="ot"> name ::</span> <span class="dt">String</span></span>
|
||||
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb2"><pre class="sourceCode haskell code-with-copy"><code class="sourceCode haskell"><span id="cb2-1"><a href="#cb2-1" aria-hidden="true" tabindex="-1"></a><span class="kw">data</span> <span class="dt">Person</span> <span class="ot">=</span> <span class="dt">P</span> {<span class="ot"> name ::</span> <span class="dt">String</span></span>
|
||||
<span id="cb2-2"><a href="#cb2-2" aria-hidden="true" tabindex="-1"></a> ,<span class="ot"> addr ::</span> <span class="dt">Address</span></span>
|
||||
<span id="cb2-3"><a href="#cb2-3" aria-hidden="true" tabindex="-1"></a> ,<span class="ot"> salary ::</span> <span class="dt">Int</span> }</span>
|
||||
<span id="cb2-4"><a href="#cb2-4" aria-hidden="true" tabindex="-1"></a><span class="co">-- a lens for each field</span></span>
|
||||
@ -731,12 +736,12 @@ interessiert sind</li>
|
||||
<span id="cb2-9"><a href="#cb2-9" aria-hidden="true" tabindex="-1"></a><span class="ot">view ::</span> <span class="dt">Lens'</span> s a <span class="ot">-></span> s <span class="ot">-></span> a</span>
|
||||
<span id="cb2-10"><a href="#cb2-10" aria-hidden="true" tabindex="-1"></a><span class="ot">set ::</span> <span class="dt">Lens'</span> s a <span class="ot">-></span> a <span class="ot">-></span> s <span class="ot">-></span> s</span>
|
||||
<span id="cb2-11"><a href="#cb2-11" aria-hidden="true" tabindex="-1"></a><span class="co">-- lens-composition</span></span>
|
||||
<span id="cb2-12"><a href="#cb2-12" aria-hidden="true" tabindex="-1"></a><span class="ot">composeL ::</span> <span class="dt">Lens'</span> s1 s2 <span class="ot">-></span> <span class="dt">Lens</span> s2 a <span class="ot">-></span> <span class="dt">Lens'</span> s1 a</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||||
<span id="cb2-12"><a href="#cb2-12" aria-hidden="true" tabindex="-1"></a><span class="ot">composeL ::</span> <span class="dt">Lens'</span> s1 s2 <span class="ot">-></span> <span class="dt">Lens</span> s2 a <span class="ot">-></span> <span class="dt">Lens'</span> s1 a</span></code></pre></div><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></div>
|
||||
</section>
|
||||
<section id="wie-uns-das-hilft" class="level3">
|
||||
<h3 class="anchored" data-anchor-id="wie-uns-das-hilft">Wie uns das hilft</h3>
|
||||
<p>Mit diesen Dingen (wenn wir sie hätten) könnte man dann</p>
|
||||
<div class="sourceCode" id="cb3"><pre class="sourceCode haskell code-with-copy"><code class="sourceCode haskell"><span id="cb3-1"><a href="#cb3-1" aria-hidden="true" tabindex="-1"></a><span class="kw">data</span> <span class="dt">Person</span> <span class="ot">=</span> <span class="dt">P</span> {<span class="ot"> name ::</span> <span class="dt">String</span></span>
|
||||
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb3"><pre class="sourceCode haskell code-with-copy"><code class="sourceCode haskell"><span id="cb3-1"><a href="#cb3-1" aria-hidden="true" tabindex="-1"></a><span class="kw">data</span> <span class="dt">Person</span> <span class="ot">=</span> <span class="dt">P</span> {<span class="ot"> name ::</span> <span class="dt">String</span></span>
|
||||
<span id="cb3-2"><a href="#cb3-2" aria-hidden="true" tabindex="-1"></a> ,<span class="ot"> addr ::</span> <span class="dt">Address</span></span>
|
||||
<span id="cb3-3"><a href="#cb3-3" aria-hidden="true" tabindex="-1"></a> ,<span class="ot"> salary ::</span> <span class="dt">Int</span> }</span>
|
||||
<span id="cb3-4"><a href="#cb3-4" aria-hidden="true" tabindex="-1"></a><span class="kw">data</span> <span class="dt">Address</span> <span class="ot">=</span> <span class="dt">A</span> {<span class="ot"> road ::</span> <span class="dt">String</span></span>
|
||||
@ -744,7 +749,7 @@ interessiert sind</li>
|
||||
<span id="cb3-6"><a href="#cb3-6" aria-hidden="true" tabindex="-1"></a> ,<span class="ot"> postcode ::</span> <span class="dt">String</span> }</span>
|
||||
<span id="cb3-7"><a href="#cb3-7" aria-hidden="true" tabindex="-1"></a><span class="ot">setPostcode ::</span> <span class="dt">String</span> <span class="ot">-></span> <span class="dt">Person</span> <span class="ot">-></span> <span class="dt">Person</span></span>
|
||||
<span id="cb3-8"><a href="#cb3-8" aria-hidden="true" tabindex="-1"></a>setPostcode pc p</span>
|
||||
<span id="cb3-9"><a href="#cb3-9" aria-hidden="true" tabindex="-1"></a> <span class="ot">=</span> set (laddr <span class="ot">`composeL`</span> lpostcode) pc p</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||||
<span id="cb3-9"><a href="#cb3-9" aria-hidden="true" tabindex="-1"></a> <span class="ot">=</span> set (laddr <span class="ot">`composeL`</span> lpostcode) pc p</span></code></pre></div><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></div>
|
||||
<p>machen und wäre fertig.</p>
|
||||
</section>
|
||||
</section>
|
||||
@ -752,12 +757,12 @@ interessiert sind</li>
|
||||
<h2 class="anchored" data-anchor-id="trivialer-ansatz">Trivialer Ansatz</h2>
|
||||
<section id="gettersetter-also-lens-methoden" class="level3">
|
||||
<h3 class="anchored" data-anchor-id="gettersetter-also-lens-methoden">Getter/Setter also Lens-Methoden</h3>
|
||||
<div class="sourceCode" id="cb4"><pre class="sourceCode haskell code-with-copy"><code class="sourceCode haskell"><span id="cb4-1"><a href="#cb4-1" aria-hidden="true" tabindex="-1"></a><span class="kw">data</span> <span class="dt">LensR</span> s a <span class="ot">=</span> <span class="dt">L</span> {<span class="ot"> viewR ::</span> s <span class="ot">-></span> a</span>
|
||||
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb4"><pre class="sourceCode haskell code-with-copy"><code class="sourceCode haskell"><span id="cb4-1"><a href="#cb4-1" aria-hidden="true" tabindex="-1"></a><span class="kw">data</span> <span class="dt">LensR</span> s a <span class="ot">=</span> <span class="dt">L</span> {<span class="ot"> viewR ::</span> s <span class="ot">-></span> a</span>
|
||||
<span id="cb4-2"><a href="#cb4-2" aria-hidden="true" tabindex="-1"></a> ,<span class="ot"> setR ::</span> a <span class="ot">-></span> s <span class="ot">-></span> s }</span>
|
||||
<span id="cb4-3"><a href="#cb4-3" aria-hidden="true" tabindex="-1"></a></span>
|
||||
<span id="cb4-4"><a href="#cb4-4" aria-hidden="true" tabindex="-1"></a>composeL (<span class="dt">L</span> v1 u1) (<span class="dt">L</span> v2 u2)</span>
|
||||
<span id="cb4-5"><a href="#cb4-5" aria-hidden="true" tabindex="-1"></a> <span class="ot">=</span> <span class="dt">L</span> (\s <span class="ot">-></span> v2 (v1 s))</span>
|
||||
<span id="cb4-6"><a href="#cb4-6" aria-hidden="true" tabindex="-1"></a> (\a s <span class="ot">-></span> u1 (u2 a (v1 s)) s)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||||
<span id="cb4-6"><a href="#cb4-6" aria-hidden="true" tabindex="-1"></a> (\a s <span class="ot">-></span> u1 (u2 a (v1 s)) s)</span></code></pre></div><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></div>
|
||||
</section>
|
||||
<section id="wieso-ist-das-schlecht" class="level3">
|
||||
<h3 class="anchored" data-anchor-id="wieso-ist-das-schlecht">Wieso ist das schlecht?</h3>
|
||||
@ -766,110 +771,110 @@ interessiert sind</li>
|
||||
</ul>
|
||||
<p>Auslesen traversiert die Datenstruktur, dann wird die Function angewendet und<br>
|
||||
zum setzen wird die Datenstruktur erneut traversiert:</p>
|
||||
<div class="sourceCode" id="cb5"><pre class="sourceCode haskell code-with-copy"><code class="sourceCode haskell"><span id="cb5-1"><a href="#cb5-1" aria-hidden="true" tabindex="-1"></a><span class="ot">over ::</span> <span class="dt">LensR</span> s a <span class="ot">-></span> (a <span class="ot">-></span> a) <span class="ot">-></span> s <span class="ot">-></span> s</span>
|
||||
<span id="cb5-2"><a href="#cb5-2" aria-hidden="true" tabindex="-1"></a>over ln f s <span class="ot">=</span> setR l (f (viewR l s)) s</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||||
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb5"><pre class="sourceCode haskell code-with-copy"><code class="sourceCode haskell"><span id="cb5-1"><a href="#cb5-1" aria-hidden="true" tabindex="-1"></a><span class="ot">over ::</span> <span class="dt">LensR</span> s a <span class="ot">-></span> (a <span class="ot">-></span> a) <span class="ot">-></span> s <span class="ot">-></span> s</span>
|
||||
<span id="cb5-2"><a href="#cb5-2" aria-hidden="true" tabindex="-1"></a>over ln f s <span class="ot">=</span> setR l (f (viewR l s)) s</span></code></pre></div><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></div>
|
||||
<ul>
|
||||
<li>Lösung: modify-funktion hinzufügen</li>
|
||||
</ul>
|
||||
<div class="sourceCode" id="cb6"><pre class="sourceCode haskell code-with-copy"><code class="sourceCode haskell"><span id="cb6-1"><a href="#cb6-1" aria-hidden="true" tabindex="-1"></a><span class="kw">data</span> <span class="dt">LensR</span> s a</span>
|
||||
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb6"><pre class="sourceCode haskell code-with-copy"><code class="sourceCode haskell"><span id="cb6-1"><a href="#cb6-1" aria-hidden="true" tabindex="-1"></a><span class="kw">data</span> <span class="dt">LensR</span> s a</span>
|
||||
<span id="cb6-2"><a href="#cb6-2" aria-hidden="true" tabindex="-1"></a> <span class="ot">=</span> <span class="dt">L</span> {<span class="ot"> viewR ::</span> s <span class="ot">-></span> a</span>
|
||||
<span id="cb6-3"><a href="#cb6-3" aria-hidden="true" tabindex="-1"></a> ,<span class="ot"> setR ::</span> a <span class="ot">-></span> s <span class="ot">-></span> s</span>
|
||||
<span id="cb6-4"><a href="#cb6-4" aria-hidden="true" tabindex="-1"></a> ,<span class="ot"> mod ::</span> (a<span class="ot">-></span>a) <span class="ot">-></span> s <span class="ot">-></span> s</span>
|
||||
<span id="cb6-5"><a href="#cb6-5" aria-hidden="true" tabindex="-1"></a> ,<span class="ot"> modM ::</span> (a<span class="ot">-></span><span class="dt">Maybe</span> a) <span class="ot">-></span> s <span class="ot">-></span> <span class="dt">Maybe</span> s</span>
|
||||
<span id="cb6-6"><a href="#cb6-6" aria-hidden="true" tabindex="-1"></a> ,<span class="ot"> modIO ::</span> (a<span class="ot">-></span><span class="dt">IO</span> a) <span class="ot">-></span> s <span class="ot">-></span> <span class="dt">IO</span> s }</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||||
<span id="cb6-6"><a href="#cb6-6" aria-hidden="true" tabindex="-1"></a> ,<span class="ot"> modIO ::</span> (a<span class="ot">-></span><span class="dt">IO</span> a) <span class="ot">-></span> s <span class="ot">-></span> <span class="dt">IO</span> s }</span></code></pre></div><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></div>
|
||||
<p>Neues Problem: Für jeden Spezialfall muss die Lens erweitert werden.</p>
|
||||
</section>
|
||||
<section id="something-in-common" class="level3">
|
||||
<h3 class="anchored" data-anchor-id="something-in-common">Something in common</h3>
|
||||
<p>Man kann alle Monaden abstrahieren. Functor reicht schon:</p>
|
||||
<div class="sourceCode" id="cb7"><pre class="sourceCode haskell code-with-copy"><code class="sourceCode haskell"><span id="cb7-1"><a href="#cb7-1" aria-hidden="true" tabindex="-1"></a><span class="kw">data</span> <span class="dt">LensR</span> s a</span>
|
||||
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb7"><pre class="sourceCode haskell code-with-copy"><code class="sourceCode haskell"><span id="cb7-1"><a href="#cb7-1" aria-hidden="true" tabindex="-1"></a><span class="kw">data</span> <span class="dt">LensR</span> s a</span>
|
||||
<span id="cb7-2"><a href="#cb7-2" aria-hidden="true" tabindex="-1"></a> <span class="ot">=</span> <span class="dt">L</span> {<span class="ot"> viewR ::</span> s <span class="ot">-></span> a</span>
|
||||
<span id="cb7-3"><a href="#cb7-3" aria-hidden="true" tabindex="-1"></a> ,<span class="ot"> setR ::</span> a <span class="ot">-></span> s <span class="ot">-></span> s</span>
|
||||
<span id="cb7-4"><a href="#cb7-4" aria-hidden="true" tabindex="-1"></a> ,<span class="ot"> mod ::</span> (a<span class="ot">-></span>a) <span class="ot">-></span> s <span class="ot">-></span> s</span>
|
||||
<span id="cb7-5"><a href="#cb7-5" aria-hidden="true" tabindex="-1"></a> ,<span class="ot"> modF ::</span> <span class="dt">Functor</span> f <span class="ot">=></span> (a<span class="ot">-></span>f a) <span class="ot">-></span> s <span class="ot">-></span> f s }</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||||
<span id="cb7-5"><a href="#cb7-5" aria-hidden="true" tabindex="-1"></a> ,<span class="ot"> modF ::</span> <span class="dt">Functor</span> f <span class="ot">=></span> (a<span class="ot">-></span>f a) <span class="ot">-></span> s <span class="ot">-></span> f s }</span></code></pre></div><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></div>
|
||||
<p>Idee: Die 3 darüberliegenden durch modF ausdrücken.</p>
|
||||
</section>
|
||||
<section id="typ-einer-lens" class="level3">
|
||||
<h3 class="anchored" data-anchor-id="typ-einer-lens">Typ einer Lens</h3>
|
||||
<p>Wenn man das berücksichtigt, dann hat einen Lens folgenden Typ:</p>
|
||||
<div class="sourceCode" id="cb8"><pre class="sourceCode haskell code-with-copy"><code class="sourceCode haskell"><span id="cb8-1"><a href="#cb8-1" aria-hidden="true" tabindex="-1"></a><span class="kw">type</span> <span class="dt">Lens'</span> s a <span class="ot">=</span> <span class="kw">forall</span> f<span class="op">.</span> <span class="dt">Functor</span> f</span>
|
||||
<span id="cb8-2"><a href="#cb8-2" aria-hidden="true" tabindex="-1"></a> <span class="ot">=></span> (a <span class="ot">-></span> f a) <span class="ot">-></span> s <span class="ot">-></span> f s</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||||
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb8"><pre class="sourceCode haskell code-with-copy"><code class="sourceCode haskell"><span id="cb8-1"><a href="#cb8-1" aria-hidden="true" tabindex="-1"></a><span class="kw">type</span> <span class="dt">Lens'</span> s a <span class="ot">=</span> <span class="kw">forall</span> f<span class="op">.</span> <span class="dt">Functor</span> f</span>
|
||||
<span id="cb8-2"><a href="#cb8-2" aria-hidden="true" tabindex="-1"></a> <span class="ot">=></span> (a <span class="ot">-></span> f a) <span class="ot">-></span> s <span class="ot">-></span> f s</span></code></pre></div><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></div>
|
||||
<p>Allerdings haben wir dann noch unseren getter/setter:</p>
|
||||
<div class="sourceCode" id="cb9"><pre class="sourceCode haskell code-with-copy"><code class="sourceCode haskell"><span id="cb9-1"><a href="#cb9-1" aria-hidden="true" tabindex="-1"></a><span class="kw">data</span> <span class="dt">LensR</span> s a <span class="ot">=</span> <span class="dt">L</span> {<span class="ot"> viewR ::</span> s <span class="ot">-></span> a</span>
|
||||
<span id="cb9-2"><a href="#cb9-2" aria-hidden="true" tabindex="-1"></a> ,<span class="ot"> setR ::</span> a <span class="ot">-></span> s <span class="ot">-></span> s }</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||||
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb9"><pre class="sourceCode haskell code-with-copy"><code class="sourceCode haskell"><span id="cb9-1"><a href="#cb9-1" aria-hidden="true" tabindex="-1"></a><span class="kw">data</span> <span class="dt">LensR</span> s a <span class="ot">=</span> <span class="dt">L</span> {<span class="ot"> viewR ::</span> s <span class="ot">-></span> a</span>
|
||||
<span id="cb9-2"><a href="#cb9-2" aria-hidden="true" tabindex="-1"></a> ,<span class="ot"> setR ::</span> a <span class="ot">-></span> s <span class="ot">-></span> s }</span></code></pre></div><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></div>
|
||||
<p>Stellt sich raus: Die sind isomorph! Auch wenn die von den Typen her komplett<br>
|
||||
anders aussehen.</p>
|
||||
</section>
|
||||
</section>
|
||||
<section id="benutzen-einer-lens-also-setter" class="level2">
|
||||
<h2 class="anchored" data-anchor-id="benutzen-einer-lens-also-setter">Benutzen einer Lens also Setter</h2>
|
||||
<div class="sourceCode" id="cb10"><pre class="sourceCode haskell code-with-copy"><code class="sourceCode haskell"><span id="cb10-1"><a href="#cb10-1" aria-hidden="true" tabindex="-1"></a><span class="ot">set ::</span> <span class="dt">Lens'</span> s a <span class="ot">-></span> (a <span class="ot">-></span> s <span class="ot">-></span> s)</span>
|
||||
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb10"><pre class="sourceCode haskell code-with-copy"><code class="sourceCode haskell"><span id="cb10-1"><a href="#cb10-1" aria-hidden="true" tabindex="-1"></a><span class="ot">set ::</span> <span class="dt">Lens'</span> s a <span class="ot">-></span> (a <span class="ot">-></span> s <span class="ot">-></span> s)</span>
|
||||
<span id="cb10-2"><a href="#cb10-2" aria-hidden="true" tabindex="-1"></a>set ln a s <span class="ot">=</span> <span class="op">--...</span>umm<span class="op">...</span></span>
|
||||
<span id="cb10-3"><a href="#cb10-3" aria-hidden="true" tabindex="-1"></a><span class="op">--:</span>t ln <span class="ot">=></span> (a <span class="ot">-></span> f a) <span class="ot">-></span> s <span class="ot">-></span> f s</span>
|
||||
<span id="cb10-4"><a href="#cb10-4" aria-hidden="true" tabindex="-1"></a><span class="co">-- => get s out of f s to return it</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||||
<span id="cb10-4"><a href="#cb10-4" aria-hidden="true" tabindex="-1"></a><span class="co">-- => get s out of f s to return it</span></span></code></pre></div><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></div>
|
||||
<p>Wir können für f einfach die “Identity”-Monade nehmen, die wir nachher wegcasten<br>
|
||||
können.</p>
|
||||
<div class="sourceCode" id="cb11"><pre class="sourceCode haskell code-with-copy"><code class="sourceCode haskell"><span id="cb11-1"><a href="#cb11-1" aria-hidden="true" tabindex="-1"></a><span class="kw">newtype</span> <span class="dt">Identity</span> a <span class="ot">=</span> <span class="dt">Identity</span> a</span>
|
||||
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb11"><pre class="sourceCode haskell code-with-copy"><code class="sourceCode haskell"><span id="cb11-1"><a href="#cb11-1" aria-hidden="true" tabindex="-1"></a><span class="kw">newtype</span> <span class="dt">Identity</span> a <span class="ot">=</span> <span class="dt">Identity</span> a</span>
|
||||
<span id="cb11-2"><a href="#cb11-2" aria-hidden="true" tabindex="-1"></a><span class="co">-- Id :: a -> Identity a</span></span>
|
||||
<span id="cb11-3"><a href="#cb11-3" aria-hidden="true" tabindex="-1"></a></span>
|
||||
<span id="cb11-4"><a href="#cb11-4" aria-hidden="true" tabindex="-1"></a><span class="ot">runIdentity ::</span> <span class="dt">Identity</span> s <span class="ot">-></span> s</span>
|
||||
<span id="cb11-5"><a href="#cb11-5" aria-hidden="true" tabindex="-1"></a>runIdentity (<span class="dt">Identity</span> x) <span class="ot">=</span> x</span>
|
||||
<span id="cb11-6"><a href="#cb11-6" aria-hidden="true" tabindex="-1"></a></span>
|
||||
<span id="cb11-7"><a href="#cb11-7" aria-hidden="true" tabindex="-1"></a><span class="kw">instance</span> <span class="dt">Functor</span> <span class="dt">Identity</span> <span class="kw">where</span></span>
|
||||
<span id="cb11-8"><a href="#cb11-8" aria-hidden="true" tabindex="-1"></a> <span class="fu">fmap</span> f (<span class="dt">Identity</span> x) <span class="ot">=</span> <span class="dt">Identity</span> (f x)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||||
<span id="cb11-8"><a href="#cb11-8" aria-hidden="true" tabindex="-1"></a> <span class="fu">fmap</span> f (<span class="dt">Identity</span> x) <span class="ot">=</span> <span class="dt">Identity</span> (f x)</span></code></pre></div><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></div>
|
||||
<p>somit ist set einfach nur</p>
|
||||
<div class="sourceCode" id="cb12"><pre class="sourceCode haskell code-with-copy"><code class="sourceCode haskell"><span id="cb12-1"><a href="#cb12-1" aria-hidden="true" tabindex="-1"></a><span class="ot">set ::</span> <span class="dt">Lens'</span> s a <span class="ot">-></span> (a <span class="ot">-></span> s <span class="ot">-></span> s)</span>
|
||||
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb12"><pre class="sourceCode haskell code-with-copy"><code class="sourceCode haskell"><span id="cb12-1"><a href="#cb12-1" aria-hidden="true" tabindex="-1"></a><span class="ot">set ::</span> <span class="dt">Lens'</span> s a <span class="ot">-></span> (a <span class="ot">-></span> s <span class="ot">-></span> s)</span>
|
||||
<span id="cb12-2"><a href="#cb12-2" aria-hidden="true" tabindex="-1"></a>set ln x s</span>
|
||||
<span id="cb12-3"><a href="#cb12-3" aria-hidden="true" tabindex="-1"></a> <span class="ot">=</span> runIdentity (ls set_fld s)</span>
|
||||
<span id="cb12-4"><a href="#cb12-4" aria-hidden="true" tabindex="-1"></a> <span class="kw">where</span></span>
|
||||
<span id="cb12-5"><a href="#cb12-5" aria-hidden="true" tabindex="-1"></a><span class="ot"> set_fld ::</span> a <span class="ot">-></span> <span class="dt">Identity</span> a</span>
|
||||
<span id="cb12-6"><a href="#cb12-6" aria-hidden="true" tabindex="-1"></a> set_fld _ <span class="ot">=</span> <span class="dt">Identity</span> x</span>
|
||||
<span id="cb12-7"><a href="#cb12-7" aria-hidden="true" tabindex="-1"></a> <span class="co">-- a was the OLD value.</span></span>
|
||||
<span id="cb12-8"><a href="#cb12-8" aria-hidden="true" tabindex="-1"></a> <span class="co">-- We throw that away and set the new value</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||||
<span id="cb12-8"><a href="#cb12-8" aria-hidden="true" tabindex="-1"></a> <span class="co">-- We throw that away and set the new value</span></span></code></pre></div><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></div>
|
||||
<p>oder kürzer (für nerds wie den Author der Lens-Lib)</p>
|
||||
<div class="sourceCode" id="cb13"><pre class="sourceCode haskell code-with-copy"><code class="sourceCode haskell"><span id="cb13-1"><a href="#cb13-1" aria-hidden="true" tabindex="-1"></a><span class="ot">set ::</span> <span class="dt">Lens'</span> s a <span class="ot">-></span> (a <span class="ot">-></span> s <span class="ot">-></span> s)</span>
|
||||
<span id="cb13-2"><a href="#cb13-2" aria-hidden="true" tabindex="-1"></a>set ln x <span class="ot">=</span> runIdentity <span class="op">.</span> ln (<span class="dt">Identity</span> <span class="op">.</span> <span class="fu">const</span> x)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||||
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb13"><pre class="sourceCode haskell code-with-copy"><code class="sourceCode haskell"><span id="cb13-1"><a href="#cb13-1" aria-hidden="true" tabindex="-1"></a><span class="ot">set ::</span> <span class="dt">Lens'</span> s a <span class="ot">-></span> (a <span class="ot">-></span> s <span class="ot">-></span> s)</span>
|
||||
<span id="cb13-2"><a href="#cb13-2" aria-hidden="true" tabindex="-1"></a>set ln x <span class="ot">=</span> runIdentity <span class="op">.</span> ln (<span class="dt">Identity</span> <span class="op">.</span> <span class="fu">const</span> x)</span></code></pre></div><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></div>
|
||||
</section>
|
||||
<section id="benutzen-einer-lens-also-modify" class="level2">
|
||||
<h2 class="anchored" data-anchor-id="benutzen-einer-lens-also-modify">Benutzen einer Lens also Modify</h2>
|
||||
<p>Dasselbe wie Set, nur dass wir den Parameter nicht entsorgen, sondern in die<br>
|
||||
mitgelieferte Function stopfen.</p>
|
||||
<div class="sourceCode" id="cb14"><pre class="sourceCode haskell code-with-copy"><code class="sourceCode haskell"><span id="cb14-1"><a href="#cb14-1" aria-hidden="true" tabindex="-1"></a><span class="ot">over ::</span> <span class="dt">Lens'</span> s a <span class="ot">-></span> (a <span class="ot">-></span> a) <span class="ot">-></span> s <span class="ot">-></span> s</span>
|
||||
<span id="cb14-2"><a href="#cb14-2" aria-hidden="true" tabindex="-1"></a>over ln f <span class="ot">=</span> runIdentity <span class="op">.</span> ln (<span class="dt">Identity</span> <span class="op">.</span> f)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||||
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb14"><pre class="sourceCode haskell code-with-copy"><code class="sourceCode haskell"><span id="cb14-1"><a href="#cb14-1" aria-hidden="true" tabindex="-1"></a><span class="ot">over ::</span> <span class="dt">Lens'</span> s a <span class="ot">-></span> (a <span class="ot">-></span> a) <span class="ot">-></span> s <span class="ot">-></span> s</span>
|
||||
<span id="cb14-2"><a href="#cb14-2" aria-hidden="true" tabindex="-1"></a>over ln f <span class="ot">=</span> runIdentity <span class="op">.</span> ln (<span class="dt">Identity</span> <span class="op">.</span> f)</span></code></pre></div><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></div>
|
||||
</section>
|
||||
<section id="benutzen-einer-lens-also-getter" class="level2">
|
||||
<h2 class="anchored" data-anchor-id="benutzen-einer-lens-also-getter">Benutzen einer Lens also Getter</h2>
|
||||
<div class="sourceCode" id="cb15"><pre class="sourceCode haskell code-with-copy"><code class="sourceCode haskell"><span id="cb15-1"><a href="#cb15-1" aria-hidden="true" tabindex="-1"></a><span class="ot">view ::</span> <span class="dt">Lens'</span> s a <span class="ot">-></span> (s <span class="ot">-></span> a)</span>
|
||||
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb15"><pre class="sourceCode haskell code-with-copy"><code class="sourceCode haskell"><span id="cb15-1"><a href="#cb15-1" aria-hidden="true" tabindex="-1"></a><span class="ot">view ::</span> <span class="dt">Lens'</span> s a <span class="ot">-></span> (s <span class="ot">-></span> a)</span>
|
||||
<span id="cb15-2"><a href="#cb15-2" aria-hidden="true" tabindex="-1"></a>view ln s <span class="ot">=</span> <span class="op">--...</span>umm<span class="op">...</span></span>
|
||||
<span id="cb15-3"><a href="#cb15-3" aria-hidden="true" tabindex="-1"></a><span class="op">--:</span>t ln <span class="ot">=></span> (a <span class="ot">-></span> f a) <span class="ot">-></span> s <span class="ot">-></span> f s</span>
|
||||
<span id="cb15-4"><a href="#cb15-4" aria-hidden="true" tabindex="-1"></a><span class="co">-- => get a out of the (f s) return-value</span></span>
|
||||
<span id="cb15-5"><a href="#cb15-5" aria-hidden="true" tabindex="-1"></a><span class="co">-- Wait, WHAT?</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||||
<span id="cb15-5"><a href="#cb15-5" aria-hidden="true" tabindex="-1"></a><span class="co">-- Wait, WHAT?</span></span></code></pre></div><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></div>
|
||||
<p>Auch hier gibt es einen netten Funktor. Wir packen das “a” einfach in das “f”<br>
|
||||
und werfen das “s” am End weg.</p>
|
||||
<div class="sourceCode" id="cb16"><pre class="sourceCode haskell code-with-copy"><code class="sourceCode haskell"><span id="cb16-1"><a href="#cb16-1" aria-hidden="true" tabindex="-1"></a><span class="kw">newtype</span> <span class="dt">Const</span> v a <span class="ot">=</span> <span class="dt">Const</span> v</span>
|
||||
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb16"><pre class="sourceCode haskell code-with-copy"><code class="sourceCode haskell"><span id="cb16-1"><a href="#cb16-1" aria-hidden="true" tabindex="-1"></a><span class="kw">newtype</span> <span class="dt">Const</span> v a <span class="ot">=</span> <span class="dt">Const</span> v</span>
|
||||
<span id="cb16-2"><a href="#cb16-2" aria-hidden="true" tabindex="-1"></a></span>
|
||||
<span id="cb16-3"><a href="#cb16-3" aria-hidden="true" tabindex="-1"></a><span class="ot">getConst ::</span> <span class="dt">Const</span> v a <span class="ot">-></span> v</span>
|
||||
<span id="cb16-4"><a href="#cb16-4" aria-hidden="true" tabindex="-1"></a>getConst (<span class="dt">Const</span> x) <span class="ot">=</span> x</span>
|
||||
<span id="cb16-5"><a href="#cb16-5" aria-hidden="true" tabindex="-1"></a></span>
|
||||
<span id="cb16-6"><a href="#cb16-6" aria-hidden="true" tabindex="-1"></a><span class="kw">instance</span> <span class="dt">Functor</span> (<span class="dt">Const</span> v) <span class="kw">where</span></span>
|
||||
<span id="cb16-7"><a href="#cb16-7" aria-hidden="true" tabindex="-1"></a> <span class="fu">fmap</span> f (<span class="dt">Const</span> x) <span class="ot">=</span> <span class="dt">Const</span> x</span>
|
||||
<span id="cb16-8"><a href="#cb16-8" aria-hidden="true" tabindex="-1"></a> <span class="co">-- throw f away. Nothing changes our const!</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||||
<span id="cb16-8"><a href="#cb16-8" aria-hidden="true" tabindex="-1"></a> <span class="co">-- throw f away. Nothing changes our const!</span></span></code></pre></div><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></div>
|
||||
<p>somit ergibt sich</p>
|
||||
<div class="sourceCode" id="cb17"><pre class="sourceCode haskell code-with-copy"><code class="sourceCode haskell"><span id="cb17-1"><a href="#cb17-1" aria-hidden="true" tabindex="-1"></a><span class="ot">view ::</span> <span class="dt">Lens'</span> s a <span class="ot">-></span> (s <span class="ot">-></span> a)</span>
|
||||
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb17"><pre class="sourceCode haskell code-with-copy"><code class="sourceCode haskell"><span id="cb17-1"><a href="#cb17-1" aria-hidden="true" tabindex="-1"></a><span class="ot">view ::</span> <span class="dt">Lens'</span> s a <span class="ot">-></span> (s <span class="ot">-></span> a)</span>
|
||||
<span id="cb17-2"><a href="#cb17-2" aria-hidden="true" tabindex="-1"></a>view ln s</span>
|
||||
<span id="cb17-3"><a href="#cb17-3" aria-hidden="true" tabindex="-1"></a> <span class="ot">=</span> getConst (ln <span class="dt">Const</span> s)</span>
|
||||
<span id="cb17-4"><a href="#cb17-4" aria-hidden="true" tabindex="-1"></a> <span class="co">-- Const :: s -> Const a s</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||||
<span id="cb17-4"><a href="#cb17-4" aria-hidden="true" tabindex="-1"></a> <span class="co">-- Const :: s -> Const a s</span></span></code></pre></div><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></div>
|
||||
<p>oder nerdig</p>
|
||||
<div class="sourceCode" id="cb18"><pre class="sourceCode haskell code-with-copy"><code class="sourceCode haskell"><span id="cb18-1"><a href="#cb18-1" aria-hidden="true" tabindex="-1"></a><span class="ot">view ::</span> <span class="dt">Lens'</span> s a <span class="ot">-></span> (s <span class="ot">-></span> a)</span>
|
||||
<span id="cb18-2"><a href="#cb18-2" aria-hidden="true" tabindex="-1"></a>view ln <span class="ot">=</span> getConst <span class="op">.</span> ln <span class="dt">Const</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||||
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb18"><pre class="sourceCode haskell code-with-copy"><code class="sourceCode haskell"><span id="cb18-1"><a href="#cb18-1" aria-hidden="true" tabindex="-1"></a><span class="ot">view ::</span> <span class="dt">Lens'</span> s a <span class="ot">-></span> (s <span class="ot">-></span> a)</span>
|
||||
<span id="cb18-2"><a href="#cb18-2" aria-hidden="true" tabindex="-1"></a>view ln <span class="ot">=</span> getConst <span class="op">.</span> ln <span class="dt">Const</span></span></code></pre></div><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></div>
|
||||
</section>
|
||||
<section id="lenses-bauen" class="level2">
|
||||
<h2 class="anchored" data-anchor-id="lenses-bauen">Lenses bauen</h2>
|
||||
<p>Nochmal kurz der Typ:</p>
|
||||
<div class="sourceCode" id="cb19"><pre class="sourceCode haskell code-with-copy"><code class="sourceCode haskell"><span id="cb19-1"><a href="#cb19-1" aria-hidden="true" tabindex="-1"></a><span class="kw">type</span> <span class="dt">Lens'</span> s a <span class="ot">=</span> <span class="kw">forall</span> f<span class="op">.</span> <span class="dt">Functor</span> f</span>
|
||||
<span id="cb19-2"><a href="#cb19-2" aria-hidden="true" tabindex="-1"></a> <span class="ot">=></span> (a <span class="ot">-></span> f a) <span class="ot">-></span> s <span class="ot">-></span> f s</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||||
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb19"><pre class="sourceCode haskell code-with-copy"><code class="sourceCode haskell"><span id="cb19-1"><a href="#cb19-1" aria-hidden="true" tabindex="-1"></a><span class="kw">type</span> <span class="dt">Lens'</span> s a <span class="ot">=</span> <span class="kw">forall</span> f<span class="op">.</span> <span class="dt">Functor</span> f</span>
|
||||
<span id="cb19-2"><a href="#cb19-2" aria-hidden="true" tabindex="-1"></a> <span class="ot">=></span> (a <span class="ot">-></span> f a) <span class="ot">-></span> s <span class="ot">-></span> f s</span></code></pre></div><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></div>
|
||||
<p>Für unser Personen-Beispiel vom Anfang:</p>
|
||||
<div class="sourceCode" id="cb20"><pre class="sourceCode haskell code-with-copy"><code class="sourceCode haskell"><span id="cb20-1"><a href="#cb20-1" aria-hidden="true" tabindex="-1"></a><span class="kw">data</span> <span class="dt">Person</span> <span class="ot">=</span> <span class="dt">P</span> {<span class="ot"> _name ::</span> <span class="dt">String</span>,<span class="ot"> _salary ::</span> <span class="dt">Int</span> }</span>
|
||||
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb20"><pre class="sourceCode haskell code-with-copy"><code class="sourceCode haskell"><span id="cb20-1"><a href="#cb20-1" aria-hidden="true" tabindex="-1"></a><span class="kw">data</span> <span class="dt">Person</span> <span class="ot">=</span> <span class="dt">P</span> {<span class="ot"> _name ::</span> <span class="dt">String</span>,<span class="ot"> _salary ::</span> <span class="dt">Int</span> }</span>
|
||||
<span id="cb20-2"><a href="#cb20-2" aria-hidden="true" tabindex="-1"></a></span>
|
||||
<span id="cb20-3"><a href="#cb20-3" aria-hidden="true" tabindex="-1"></a><span class="ot">name ::</span> <span class="dt">Lens'</span> <span class="dt">Person</span> <span class="dt">String</span></span>
|
||||
<span id="cb20-4"><a href="#cb20-4" aria-hidden="true" tabindex="-1"></a><span class="co">-- name :: Functor f => (String -> f String)</span></span>
|
||||
@ -879,15 +884,15 @@ und werfen das “s” am End weg.</p>
|
||||
<span id="cb20-8"><a href="#cb20-8" aria-hidden="true" tabindex="-1"></a> <span class="ot">=</span> <span class="fu">fmap</span> (\n' <span class="ot">-></span> <span class="dt">P</span> n' s) (elt_fn n)</span>
|
||||
<span id="cb20-9"><a href="#cb20-9" aria-hidden="true" tabindex="-1"></a><span class="co">-- fmap :: Functor f => (a->b) -> f a -> f b - der Funktor, der alles verknüpft</span></span>
|
||||
<span id="cb20-10"><a href="#cb20-10" aria-hidden="true" tabindex="-1"></a><span class="co">-- \n' -> .. :: String -> Person - Funktion um das Element zu lokalisieren (WO wird ersetzt/gelesen/...)</span></span>
|
||||
<span id="cb20-11"><a href="#cb20-11" aria-hidden="true" tabindex="-1"></a><span class="co">-- elt_fn n :: f String - Funktion um das Element zu verändern (setzen, ändern, ...)</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||||
<span id="cb20-11"><a href="#cb20-11" aria-hidden="true" tabindex="-1"></a><span class="co">-- elt_fn n :: f String - Funktion um das Element zu verändern (setzen, ändern, ...)</span></span></code></pre></div><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></div>
|
||||
<p>Die Lambda-Funktion ersetzt einfach den Namen. Häufig sieht man auch</p>
|
||||
<div class="sourceCode" id="cb21"><pre class="sourceCode haskell code-with-copy"><code class="sourceCode haskell"><span id="cb21-1"><a href="#cb21-1" aria-hidden="true" tabindex="-1"></a>name elt_fn (<span class="dt">P</span> n s)</span>
|
||||
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb21"><pre class="sourceCode haskell code-with-copy"><code class="sourceCode haskell"><span id="cb21-1"><a href="#cb21-1" aria-hidden="true" tabindex="-1"></a>name elt_fn (<span class="dt">P</span> n s)</span>
|
||||
<span id="cb21-2"><a href="#cb21-2" aria-hidden="true" tabindex="-1"></a> <span class="ot">=</span> (\n' <span class="ot">-></span> <span class="dt">P</span> n' s) <span class="op"><$></span> (elt_fn n)</span>
|
||||
<span id="cb21-3"><a href="#cb21-3" aria-hidden="true" tabindex="-1"></a><span class="co">-- | Focus | |Function|</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||||
<span id="cb21-3"><a href="#cb21-3" aria-hidden="true" tabindex="-1"></a><span class="co">-- | Focus | |Function|</span></span></code></pre></div><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></div>
|
||||
</section>
|
||||
<section id="wie-funktioniert-das-intern" class="level2">
|
||||
<h2 class="anchored" data-anchor-id="wie-funktioniert-das-intern">Wie funktioniert das intern?</h2>
|
||||
<div class="sourceCode" id="cb22"><pre class="sourceCode haskell code-with-copy"><code class="sourceCode haskell"><span id="cb22-1"><a href="#cb22-1" aria-hidden="true" tabindex="-1"></a>view name (<span class="dt">P</span> {_name<span class="ot">=</span><span class="st">"Fred"</span>, _salary<span class="ot">=</span><span class="dv">100</span>})</span>
|
||||
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb22"><pre class="sourceCode haskell code-with-copy"><code class="sourceCode haskell"><span id="cb22-1"><a href="#cb22-1" aria-hidden="true" tabindex="-1"></a>view name (<span class="dt">P</span> {_name<span class="ot">=</span><span class="st">"Fred"</span>, _salary<span class="ot">=</span><span class="dv">100</span>})</span>
|
||||
<span id="cb22-2"><a href="#cb22-2" aria-hidden="true" tabindex="-1"></a> <span class="co">-- inline view-function</span></span>
|
||||
<span id="cb22-3"><a href="#cb22-3" aria-hidden="true" tabindex="-1"></a><span class="ot">=</span> getConst (name <span class="dt">Const</span> (<span class="dt">P</span> {_name<span class="ot">=</span><span class="st">"Fred"</span>, _salary<span class="ot">=</span><span class="dv">100</span>})</span>
|
||||
<span id="cb22-4"><a href="#cb22-4" aria-hidden="true" tabindex="-1"></a> <span class="co">-- inline name</span></span>
|
||||
@ -895,7 +900,7 @@ und werfen das “s” am End weg.</p>
|
||||
<span id="cb22-6"><a href="#cb22-6" aria-hidden="true" tabindex="-1"></a> <span class="co">-- fmap f (Const x) = Const x - Definition von Const</span></span>
|
||||
<span id="cb22-7"><a href="#cb22-7" aria-hidden="true" tabindex="-1"></a><span class="ot">=</span> getConst (<span class="dt">Const</span> <span class="st">"Fred"</span>)</span>
|
||||
<span id="cb22-8"><a href="#cb22-8" aria-hidden="true" tabindex="-1"></a> <span class="co">-- getConst (Const x) = x</span></span>
|
||||
<span id="cb22-9"><a href="#cb22-9" aria-hidden="true" tabindex="-1"></a><span class="ot">=</span> <span class="st">"Fred"</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||||
<span id="cb22-9"><a href="#cb22-9" aria-hidden="true" tabindex="-1"></a><span class="ot">=</span> <span class="st">"Fred"</span></span></code></pre></div><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></div>
|
||||
<p>Dieser Aufruf hat KEINE Runtime-Kosten, weil der Compiler direkt die Address des<br>
|
||||
Feldes einsetzen kann. Der gesamte Boilerplate-Code wird vom Compiler<br>
|
||||
wegoptimiert.</p>
|
||||
@ -923,15 +928,15 @@ Somit ist Lens-Composition einfach nur Function-Composition (.).</p>
|
||||
<section id="automatisieren-mit-template-haskell" class="level2">
|
||||
<h2 class="anchored" data-anchor-id="automatisieren-mit-template-haskell">Automatisieren mit Template-Haskell</h2>
|
||||
<p>Der Code um die Lenses zu bauen ist für records immer Identisch:</p>
|
||||
<div class="sourceCode" id="cb23"><pre class="sourceCode haskell code-with-copy"><code class="sourceCode haskell"><span id="cb23-1"><a href="#cb23-1" aria-hidden="true" tabindex="-1"></a><span class="kw">data</span> <span class="dt">Person</span> <span class="ot">=</span> <span class="dt">P</span> {<span class="ot"> _name ::</span> <span class="dt">String</span>,<span class="ot"> _salary ::</span> <span class="dt">Int</span> }</span>
|
||||
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb23"><pre class="sourceCode haskell code-with-copy"><code class="sourceCode haskell"><span id="cb23-1"><a href="#cb23-1" aria-hidden="true" tabindex="-1"></a><span class="kw">data</span> <span class="dt">Person</span> <span class="ot">=</span> <span class="dt">P</span> {<span class="ot"> _name ::</span> <span class="dt">String</span>,<span class="ot"> _salary ::</span> <span class="dt">Int</span> }</span>
|
||||
<span id="cb23-2"><a href="#cb23-2" aria-hidden="true" tabindex="-1"></a></span>
|
||||
<span id="cb23-3"><a href="#cb23-3" aria-hidden="true" tabindex="-1"></a><span class="ot">name ::</span> <span class="dt">Lens'</span> <span class="dt">Person</span> <span class="dt">String</span></span>
|
||||
<span id="cb23-4"><a href="#cb23-4" aria-hidden="true" tabindex="-1"></a>name elt_fn (<span class="dt">P</span> n s) <span class="ot">=</span> (\n' <span class="ot">-></span> <span class="dt">P</span> n' s) <span class="op"><$></span> (elt_fn n)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||||
<span id="cb23-4"><a href="#cb23-4" aria-hidden="true" tabindex="-1"></a>name elt_fn (<span class="dt">P</span> n s) <span class="ot">=</span> (\n' <span class="ot">-></span> <span class="dt">P</span> n' s) <span class="op"><$></span> (elt_fn n)</span></code></pre></div><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></div>
|
||||
<p>Daher kann man einfach</p>
|
||||
<div class="sourceCode" id="cb24"><pre class="sourceCode haskell code-with-copy"><code class="sourceCode haskell"><span id="cb24-1"><a href="#cb24-1" aria-hidden="true" tabindex="-1"></a><span class="kw">import</span> <span class="dt">Control.Lens.TH</span></span>
|
||||
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb24"><pre class="sourceCode haskell code-with-copy"><code class="sourceCode haskell"><span id="cb24-1"><a href="#cb24-1" aria-hidden="true" tabindex="-1"></a><span class="kw">import</span> <span class="dt">Control.Lens.TH</span></span>
|
||||
<span id="cb24-2"><a href="#cb24-2" aria-hidden="true" tabindex="-1"></a><span class="kw">data</span> <span class="dt">Person</span> <span class="ot">=</span> <span class="dt">P</span> {<span class="ot"> _name ::</span> <span class="dt">String</span>,<span class="ot"> _salary ::</span> <span class="dt">Int</span> }</span>
|
||||
<span id="cb24-3"><a href="#cb24-3" aria-hidden="true" tabindex="-1"></a></span>
|
||||
<span id="cb24-4"><a href="#cb24-4" aria-hidden="true" tabindex="-1"></a><span class="op">$</span>(makeLenses '<span class="dt">'Person</span>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||||
<span id="cb24-4"><a href="#cb24-4" aria-hidden="true" tabindex="-1"></a><span class="op">$</span>(makeLenses '<span class="dt">'Person</span>)</span></code></pre></div><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></div>
|
||||
<p>nehmen, was einem eine Lens für “name” und eine Lens für “salary” generiert.<br>
|
||||
Mit anderen Templates kann man auch weitere Dinge steuern (etwa wofür Lenses<br>
|
||||
generiert werden, welches Prefix (statt _) man haben will etc. pp.).</p>
|
||||
@ -939,7 +944,7 @@ generiert werden, welches Prefix (statt _) man haben will etc. pp.).</p>
|
||||
</section>
|
||||
<section id="lenses-für-den-beispielcode" class="level2">
|
||||
<h2 class="anchored" data-anchor-id="lenses-für-den-beispielcode">Lenses für den Beispielcode</h2>
|
||||
<div class="sourceCode" id="cb25"><pre class="sourceCode haskell code-with-copy"><code class="sourceCode haskell"><span id="cb25-1"><a href="#cb25-1" aria-hidden="true" tabindex="-1"></a><span class="kw">import</span> <span class="dt">Control.Lens.TH</span></span>
|
||||
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb25"><pre class="sourceCode haskell code-with-copy"><code class="sourceCode haskell"><span id="cb25-1"><a href="#cb25-1" aria-hidden="true" tabindex="-1"></a><span class="kw">import</span> <span class="dt">Control.Lens.TH</span></span>
|
||||
<span id="cb25-2"><a href="#cb25-2" aria-hidden="true" tabindex="-1"></a></span>
|
||||
<span id="cb25-3"><a href="#cb25-3" aria-hidden="true" tabindex="-1"></a><span class="kw">data</span> <span class="dt">Person</span> <span class="ot">=</span> <span class="dt">P</span> {<span class="ot"> _name ::</span> <span class="dt">String</span></span>
|
||||
<span id="cb25-4"><a href="#cb25-4" aria-hidden="true" tabindex="-1"></a> ,<span class="ot"> _addr ::</span> <span class="dt">Address</span></span>
|
||||
@ -952,11 +957,11 @@ generiert werden, welches Prefix (statt _) man haben will etc. pp.).</p>
|
||||
<span id="cb25-11"><a href="#cb25-11" aria-hidden="true" tabindex="-1"></a><span class="op">$</span>(makeLenses '<span class="dt">'Address</span>)</span>
|
||||
<span id="cb25-12"><a href="#cb25-12" aria-hidden="true" tabindex="-1"></a></span>
|
||||
<span id="cb25-13"><a href="#cb25-13" aria-hidden="true" tabindex="-1"></a><span class="ot">setPostcode ::</span> <span class="dt">String</span> <span class="ot">-></span> <span class="dt">Person</span> <span class="ot">-></span> <span class="dt">Person</span></span>
|
||||
<span id="cb25-14"><a href="#cb25-14" aria-hidden="true" tabindex="-1"></a>setPostcode pc p <span class="ot">=</span> set (addr <span class="op">.</span> postcode) pc p</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||||
<span id="cb25-14"><a href="#cb25-14" aria-hidden="true" tabindex="-1"></a>setPostcode pc p <span class="ot">=</span> set (addr <span class="op">.</span> postcode) pc p</span></code></pre></div><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></div>
|
||||
</section>
|
||||
<section id="shortcuts-mit-line-noise" class="level2">
|
||||
<h2 class="anchored" data-anchor-id="shortcuts-mit-line-noise">Shortcuts mit “Line-Noise”</h2>
|
||||
<div class="sourceCode" id="cb26"><pre class="sourceCode haskell code-with-copy"><code class="sourceCode haskell"><span id="cb26-1"><a href="#cb26-1" aria-hidden="true" tabindex="-1"></a><span class="co">-- ...</span></span>
|
||||
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb26"><pre class="sourceCode haskell code-with-copy"><code class="sourceCode haskell"><span id="cb26-1"><a href="#cb26-1" aria-hidden="true" tabindex="-1"></a><span class="co">-- ...</span></span>
|
||||
<span id="cb26-2"><a href="#cb26-2" aria-hidden="true" tabindex="-1"></a></span>
|
||||
<span id="cb26-3"><a href="#cb26-3" aria-hidden="true" tabindex="-1"></a><span class="ot">setPostcode ::</span> <span class="dt">String</span> <span class="ot">-></span> <span class="dt">Person</span> <span class="ot">-></span> <span class="dt">Person</span></span>
|
||||
<span id="cb26-4"><a href="#cb26-4" aria-hidden="true" tabindex="-1"></a>setPostcode pc p <span class="ot">=</span> addr <span class="op">.</span> postcode <span class="op">.~</span> pc <span class="op">$</span> p</span>
|
||||
@ -964,7 +969,7 @@ generiert werden, welches Prefix (statt _) man haben will etc. pp.).</p>
|
||||
<span id="cb26-6"><a href="#cb26-6" aria-hidden="true" tabindex="-1"></a></span>
|
||||
<span id="cb26-7"><a href="#cb26-7" aria-hidden="true" tabindex="-1"></a><span class="ot">getPostcode ::</span> <span class="dt">Person</span> <span class="ot">-></span> <span class="dt">String</span></span>
|
||||
<span id="cb26-8"><a href="#cb26-8" aria-hidden="true" tabindex="-1"></a>getPostcode p <span class="ot">=</span> p <span class="op">^.</span> <span class="op">$</span> addr <span class="op">.</span> postcode</span>
|
||||
<span id="cb26-9"><a href="#cb26-9" aria-hidden="true" tabindex="-1"></a><span class="co">-- |from|get| Focus |</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||||
<span id="cb26-9"><a href="#cb26-9" aria-hidden="true" tabindex="-1"></a><span class="co">-- |from|get| Focus |</span></span></code></pre></div><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></div>
|
||||
<p>Es gibt drölf-zillionen weitere Infix-Operatoren (für Folds,<br>
|
||||
Listenkonvertierungen, -traversierungen, …)</p>
|
||||
</section>
|
||||
@ -972,7 +977,7 @@ Listenkonvertierungen, -traversierungen, …)</p>
|
||||
<h2 class="anchored" data-anchor-id="virtuelle-felder">Virtuelle Felder</h2>
|
||||
<p>Man kann mit Lenses sogar Felder emulieren, die gar nicht da sind. Angenommen<br>
|
||||
folgender Code:</p>
|
||||
<div class="sourceCode" id="cb27"><pre class="sourceCode haskell code-with-copy"><code class="sourceCode haskell"><span id="cb27-1"><a href="#cb27-1" aria-hidden="true" tabindex="-1"></a><span class="kw">data</span> <span class="dt">Temp</span> <span class="ot">=</span> <span class="dt">T</span> {<span class="ot"> _fahrenheit ::</span> <span class="dt">Float</span> }</span>
|
||||
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb27"><pre class="sourceCode haskell code-with-copy"><code class="sourceCode haskell"><span id="cb27-1"><a href="#cb27-1" aria-hidden="true" tabindex="-1"></a><span class="kw">data</span> <span class="dt">Temp</span> <span class="ot">=</span> <span class="dt">T</span> {<span class="ot"> _fahrenheit ::</span> <span class="dt">Float</span> }</span>
|
||||
<span id="cb27-2"><a href="#cb27-2" aria-hidden="true" tabindex="-1"></a></span>
|
||||
<span id="cb27-3"><a href="#cb27-3" aria-hidden="true" tabindex="-1"></a><span class="op">$</span>(makeLenses '<span class="dt">'Temp</span>)</span>
|
||||
<span id="cb27-4"><a href="#cb27-4" aria-hidden="true" tabindex="-1"></a><span class="co">-- liefert Lens: fahrenheit :: Lens Temp Float</span></span>
|
||||
@ -981,7 +986,7 @@ folgender Code:</p>
|
||||
<span id="cb27-7"><a href="#cb27-7" aria-hidden="true" tabindex="-1"></a>centigrade centi_fn (<span class="dt">T</span> faren)</span>
|
||||
<span id="cb27-8"><a href="#cb27-8" aria-hidden="true" tabindex="-1"></a> <span class="ot">=</span> (\centi' <span class="ot">-></span> <span class="dt">T</span> (cToF centi'))</span>
|
||||
<span id="cb27-9"><a href="#cb27-9" aria-hidden="true" tabindex="-1"></a> <span class="op"><$></span> (centi_fn (fToC faren))</span>
|
||||
<span id="cb27-10"><a href="#cb27-10" aria-hidden="true" tabindex="-1"></a><span class="co">-- cToF & fToC as Converter-Functions defined someplace else</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||||
<span id="cb27-10"><a href="#cb27-10" aria-hidden="true" tabindex="-1"></a><span class="co">-- cToF & fToC as Converter-Functions defined someplace else</span></span></code></pre></div><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></div>
|
||||
<p>Hiermit kann man dann auch Funktionen, die auf Grad-Celsius rechnen auf Daten<br>
|
||||
anwenden, die eigenlich nur Fahrenheit speichern, aber eine Umrechnung<br>
|
||||
bereitstellen. Analog kann man auch einen Zeit-Datentypen definieren, der<br>
|
||||
@ -992,7 +997,7 @@ Minuten oder 37 Stunden ist)</p>
|
||||
<h2 class="anchored" data-anchor-id="non-record-strukturen">Non-Record Strukturen</h2>
|
||||
<p>Das ganze kann man auch parametrisieren und auf Non-Record-Strukturen anwenden.<br>
|
||||
Beispielhaft an einer Map verdeutlicht:</p>
|
||||
<div class="sourceCode" id="cb28"><pre class="sourceCode haskell code-with-copy"><code class="sourceCode haskell"><span id="cb28-1"><a href="#cb28-1" aria-hidden="true" tabindex="-1"></a><span class="co">-- from Data.Lens.At</span></span>
|
||||
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb28"><pre class="sourceCode haskell code-with-copy"><code class="sourceCode haskell"><span id="cb28-1"><a href="#cb28-1" aria-hidden="true" tabindex="-1"></a><span class="co">-- from Data.Lens.At</span></span>
|
||||
<span id="cb28-2"><a href="#cb28-2" aria-hidden="true" tabindex="-1"></a><span class="ot">at ::</span> <span class="dt">Ord</span> k <span class="ot">=></span> k <span class="ot">-></span> <span class="dt">Lens'</span> (<span class="dt">Map</span> k v) (<span class="dt">Maybe</span> v)</span>
|
||||
<span id="cb28-3"><a href="#cb28-3" aria-hidden="true" tabindex="-1"></a></span>
|
||||
<span id="cb28-4"><a href="#cb28-4" aria-hidden="true" tabindex="-1"></a><span class="co">-- oder identisch, wenn man die Lens' auflöst:</span></span>
|
||||
@ -1009,18 +1014,18 @@ Beispielhaft an einer Map verdeutlicht:</p>
|
||||
<span id="cb28-15"><a href="#cb28-15" aria-hidden="true" tabindex="-1"></a> <span class="dt">Nothing</span> <span class="ot">-></span> m</span>
|
||||
<span id="cb28-16"><a href="#cb28-16" aria-hidden="true" tabindex="-1"></a> <span class="dt">Just</span> _ <span class="ot">-></span> Map.delete k m</span>
|
||||
<span id="cb28-17"><a href="#cb28-17" aria-hidden="true" tabindex="-1"></a></span>
|
||||
<span id="cb28-18"><a href="#cb28-18" aria-hidden="true" tabindex="-1"></a><span class="co">-- mb_fn :: Maybe v -> f Maybe v</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||||
<span id="cb28-18"><a href="#cb28-18" aria-hidden="true" tabindex="-1"></a><span class="co">-- mb_fn :: Maybe v -> f Maybe v</span></span></code></pre></div><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></div>
|
||||
</section>
|
||||
<section id="weitere-beispiele" class="level2">
|
||||
<h2 class="anchored" data-anchor-id="weitere-beispiele">Weitere Beispiele</h2>
|
||||
<ul>
|
||||
<li><p>Bitfields auf Strukturen die Bits haben (Ints, …) in Data.Bits.Lens</p></li>
|
||||
<li><p>Web-scraper in Package hexpat-lens</p>
|
||||
<div class="sourceCode" id="cb29"><pre class="sourceCode haskell code-with-copy"><code class="sourceCode haskell"><span id="cb29-1"><a href="#cb29-1" aria-hidden="true" tabindex="-1"></a>p <span class="op">^..</span> _HTML' <span class="op">.</span> to allNodes</span>
|
||||
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb29"><pre class="sourceCode haskell code-with-copy"><code class="sourceCode haskell"><span id="cb29-1"><a href="#cb29-1" aria-hidden="true" tabindex="-1"></a>p <span class="op">^..</span> _HTML' <span class="op">.</span> to allNodes</span>
|
||||
<span id="cb29-2"><a href="#cb29-2" aria-hidden="true" tabindex="-1"></a> <span class="op">.</span> <span class="fu">traverse</span> <span class="op">.</span> named <span class="st">"a"</span></span>
|
||||
<span id="cb29-3"><a href="#cb29-3" aria-hidden="true" tabindex="-1"></a> <span class="op">.</span> <span class="fu">traverse</span> <span class="op">.</span> ix <span class="st">"href"</span></span>
|
||||
<span id="cb29-4"><a href="#cb29-4" aria-hidden="true" tabindex="-1"></a> <span class="op">.</span> filtered isLocal</span>
|
||||
<span id="cb29-5"><a href="#cb29-5" aria-hidden="true" tabindex="-1"></a> <span class="op">.</span> to trimSpaces</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||||
<span id="cb29-5"><a href="#cb29-5" aria-hidden="true" tabindex="-1"></a> <span class="op">.</span> to trimSpaces</span></code></pre></div><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></div>
|
||||
<p>Zieht alle externen Links aus dem gegebenen HTML-Code in p um weitere ziele<br>
|
||||
fürs crawlen zu finden.</p></li>
|
||||
</ul>
|
||||
@ -1029,36 +1034,36 @@ fürs crawlen zu finden.</p></li>
|
||||
<h2 class="anchored" data-anchor-id="erweiterungen">Erweiterungen</h2>
|
||||
<p>Bisher hatten wir Lenses nur auf Funktoren F. Die nächstmächtigere Klasse ist<br>
|
||||
Applicative.</p>
|
||||
<div class="sourceCode" id="cb30"><pre class="sourceCode haskell code-with-copy"><code class="sourceCode haskell"><span id="cb30-1"><a href="#cb30-1" aria-hidden="true" tabindex="-1"></a><span class="kw">type</span> <span class="dt">Traversal'</span> s a <span class="ot">=</span> <span class="kw">forall</span> f<span class="op">.</span> <span class="dt">Applicative</span> f</span>
|
||||
<span id="cb30-2"><a href="#cb30-2" aria-hidden="true" tabindex="-1"></a> <span class="ot">=></span> (a <span class="ot">-></span> f a) <span class="ot">-></span> (s <span class="ot">-></span> f s)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||||
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb30"><pre class="sourceCode haskell code-with-copy"><code class="sourceCode haskell"><span id="cb30-1"><a href="#cb30-1" aria-hidden="true" tabindex="-1"></a><span class="kw">type</span> <span class="dt">Traversal'</span> s a <span class="ot">=</span> <span class="kw">forall</span> f<span class="op">.</span> <span class="dt">Applicative</span> f</span>
|
||||
<span id="cb30-2"><a href="#cb30-2" aria-hidden="true" tabindex="-1"></a> <span class="ot">=></span> (a <span class="ot">-></span> f a) <span class="ot">-></span> (s <span class="ot">-></span> f s)</span></code></pre></div><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></div>
|
||||
<p>Da wir den Container identisch lassen (weder s noch a wurde angefasst) muss sich<br>
|
||||
etwas anderes ändern. Statt eines einzelnen Focus erhalten wir viele Foci.</p>
|
||||
<p>Was ist ein Applicative überhaupt? Eine schwächere Monade (nur 1x Anwendung und<br>
|
||||
kein Bind - dafür kann man die beliebig oft hintereinanderhängen).</p>
|
||||
<div class="sourceCode" id="cb31"><pre class="sourceCode haskell code-with-copy"><code class="sourceCode haskell"><span id="cb31-1"><a href="#cb31-1" aria-hidden="true" tabindex="-1"></a><span class="kw">class</span> <span class="dt">Functor</span> f <span class="ot">=></span> <span class="dt">Applicative</span> f <span class="kw">where</span></span>
|
||||
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb31"><pre class="sourceCode haskell code-with-copy"><code class="sourceCode haskell"><span id="cb31-1"><a href="#cb31-1" aria-hidden="true" tabindex="-1"></a><span class="kw">class</span> <span class="dt">Functor</span> f <span class="ot">=></span> <span class="dt">Applicative</span> f <span class="kw">where</span></span>
|
||||
<span id="cb31-2"><a href="#cb31-2" aria-hidden="true" tabindex="-1"></a><span class="ot"> pure ::</span> a <span class="ot">-></span> f a</span>
|
||||
<span id="cb31-3"><a href="#cb31-3" aria-hidden="true" tabindex="-1"></a><span class="ot"> (<*>) ::</span> f (a <span class="ot">-></span> b) <span class="ot">-></span> f a <span class="ot">-></span> f b</span>
|
||||
<span id="cb31-4"><a href="#cb31-4" aria-hidden="true" tabindex="-1"></a></span>
|
||||
<span id="cb31-5"><a href="#cb31-5" aria-hidden="true" tabindex="-1"></a><span class="co">-- Monade als Applicative:</span></span>
|
||||
<span id="cb31-6"><a href="#cb31-6" aria-hidden="true" tabindex="-1"></a><span class="fu">pure</span> <span class="ot">=</span> <span class="fu">return</span></span>
|
||||
<span id="cb31-7"><a href="#cb31-7" aria-hidden="true" tabindex="-1"></a>mf <span class="op"><*></span> mx <span class="ot">=</span> <span class="kw">do</span> { f <span class="ot"><-</span> mf; x <span class="ot"><-</span> mx; <span class="fu">return</span> (f x) }</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||||
<span id="cb31-7"><a href="#cb31-7" aria-hidden="true" tabindex="-1"></a>mf <span class="op"><*></span> mx <span class="ot">=</span> <span class="kw">do</span> { f <span class="ot"><-</span> mf; x <span class="ot"><-</span> mx; <span class="fu">return</span> (f x) }</span></code></pre></div><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></div>
|
||||
<p>Recap: Was macht eine Lens:</p>
|
||||
<div class="sourceCode" id="cb32"><pre class="sourceCode haskell code-with-copy"><code class="sourceCode haskell"><span id="cb32-1"><a href="#cb32-1" aria-hidden="true" tabindex="-1"></a><span class="kw">data</span> <span class="dt">Adress</span> <span class="ot">=</span> <span class="dt">A</span> {<span class="ot"> _road ::</span> <span class="dt">String</span></span>
|
||||
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb32"><pre class="sourceCode haskell code-with-copy"><code class="sourceCode haskell"><span id="cb32-1"><a href="#cb32-1" aria-hidden="true" tabindex="-1"></a><span class="kw">data</span> <span class="dt">Adress</span> <span class="ot">=</span> <span class="dt">A</span> {<span class="ot"> _road ::</span> <span class="dt">String</span></span>
|
||||
<span id="cb32-2"><a href="#cb32-2" aria-hidden="true" tabindex="-1"></a> ,<span class="ot"> _city ::</span> <span class="dt">String</span></span>
|
||||
<span id="cb32-3"><a href="#cb32-3" aria-hidden="true" tabindex="-1"></a> ,<span class="ot"> _postcode ::</span> <span class="dt">String</span> }</span>
|
||||
<span id="cb32-4"><a href="#cb32-4" aria-hidden="true" tabindex="-1"></a></span>
|
||||
<span id="cb32-5"><a href="#cb32-5" aria-hidden="true" tabindex="-1"></a><span class="ot">road ::</span> <span class="dt">Lens'</span> <span class="dt">Adress</span> <span class="dt">String</span></span>
|
||||
<span id="cb32-6"><a href="#cb32-6" aria-hidden="true" tabindex="-1"></a>road elt_fn (<span class="dt">A</span> r c p) <span class="ot">=</span> (\r' <span class="ot">-></span> <span class="dt">A</span> r' c p) <span class="op"><$></span> (elt_fn r)</span>
|
||||
<span id="cb32-7"><a href="#cb32-7" aria-hidden="true" tabindex="-1"></a><span class="co">-- | "Hole" | | Thing to put in|</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||||
<span id="cb32-7"><a href="#cb32-7" aria-hidden="true" tabindex="-1"></a><span class="co">-- | "Hole" | | Thing to put in|</span></span></code></pre></div><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></div>
|
||||
<p>Wenn man nun road & city gleichzeitig bearbeiten will:</p>
|
||||
<div class="sourceCode" id="cb33"><pre class="sourceCode haskell code-with-copy"><code class="sourceCode haskell"><span id="cb33-1"><a href="#cb33-1" aria-hidden="true" tabindex="-1"></a><span class="ot">addr_strs ::</span> <span class="dt">Traversal'</span> <span class="dt">Address</span> <span class="dt">String</span></span>
|
||||
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb33"><pre class="sourceCode haskell code-with-copy"><code class="sourceCode haskell"><span id="cb33-1"><a href="#cb33-1" aria-hidden="true" tabindex="-1"></a><span class="ot">addr_strs ::</span> <span class="dt">Traversal'</span> <span class="dt">Address</span> <span class="dt">String</span></span>
|
||||
<span id="cb33-2"><a href="#cb33-2" aria-hidden="true" tabindex="-1"></a>addr_strs elt_fn (<span class="dt">A</span> r c p)</span>
|
||||
<span id="cb33-3"><a href="#cb33-3" aria-hidden="true" tabindex="-1"></a> <span class="ot">=</span> <span class="op">...</span> (\r' c' <span class="ot">-></span> <span class="dt">A</span> r' c' p) <span class="op">..</span> (elt_fn r) <span class="op">..</span> (elt_fn c) <span class="op">..</span></span>
|
||||
<span id="cb33-4"><a href="#cb33-4" aria-hidden="true" tabindex="-1"></a><span class="co">-- | function with 2 "Holes"| first Thing | second Thing</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||||
<span id="cb33-4"><a href="#cb33-4" aria-hidden="true" tabindex="-1"></a><span class="co">-- | function with 2 "Holes"| first Thing | second Thing</span></span></code></pre></div><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></div>
|
||||
<p>fmap kann nur 1 Loch stopfen, aber nicht mit n Löchern umgehen. Applicative mit<br>
|
||||
<*> kann das.<br>
|
||||
Somit gibt sich</p>
|
||||
<div class="sourceCode" id="cb34"><pre class="sourceCode haskell code-with-copy"><code class="sourceCode haskell"><span id="cb34-1"><a href="#cb34-1" aria-hidden="true" tabindex="-1"></a><span class="ot">addr_strs ::</span> <span class="dt">Traversal'</span> <span class="dt">Address</span> <span class="dt">String</span></span>
|
||||
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb34"><pre class="sourceCode haskell code-with-copy"><code class="sourceCode haskell"><span id="cb34-1"><a href="#cb34-1" aria-hidden="true" tabindex="-1"></a><span class="ot">addr_strs ::</span> <span class="dt">Traversal'</span> <span class="dt">Address</span> <span class="dt">String</span></span>
|
||||
<span id="cb34-2"><a href="#cb34-2" aria-hidden="true" tabindex="-1"></a>addr_strs elt_fn (<span class="dt">A</span> r c p)</span>
|
||||
<span id="cb34-3"><a href="#cb34-3" aria-hidden="true" tabindex="-1"></a> <span class="ot">=</span> <span class="fu">pure</span> (\r' c' <span class="ot">-></span> <span class="dt">A</span> r' c' p) <span class="op"><*></span> (elt_fn r) <span class="op"><*></span> (elt_fn c)</span>
|
||||
<span id="cb34-4"><a href="#cb34-4" aria-hidden="true" tabindex="-1"></a><span class="co">-- lift in Appl. | function with 2 "Holes"| first Thing | second Thing</span></span>
|
||||
@ -1066,13 +1071,13 @@ Somit gibt sich</p>
|
||||
<span id="cb34-6"><a href="#cb34-6" aria-hidden="true" tabindex="-1"></a><span class="ot">addr_strs ::</span> <span class="dt">Traversal'</span> <span class="dt">Address</span> <span class="dt">String</span></span>
|
||||
<span id="cb34-7"><a href="#cb34-7" aria-hidden="true" tabindex="-1"></a>addr_strs elt_fn (<span class="dt">A</span> r c p)</span>
|
||||
<span id="cb34-8"><a href="#cb34-8" aria-hidden="true" tabindex="-1"></a> <span class="ot">=</span> (\r' c' <span class="ot">-></span> <span class="dt">A</span> r' c' p) <span class="op"><$></span> (elt_fn r) <span class="op"><*></span> (elt_fn c)</span>
|
||||
<span id="cb34-9"><a href="#cb34-9" aria-hidden="true" tabindex="-1"></a><span class="co">-- pure x <*> y == x <$> y</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||||
<span id="cb34-9"><a href="#cb34-9" aria-hidden="true" tabindex="-1"></a><span class="co">-- pure x <*> y == x <$> y</span></span></code></pre></div><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></div>
|
||||
<p>Wie würd eine modify-funktion aussehen?</p>
|
||||
<div class="sourceCode" id="cb35"><pre class="sourceCode haskell code-with-copy"><code class="sourceCode haskell"><span id="cb35-1"><a href="#cb35-1" aria-hidden="true" tabindex="-1"></a><span class="ot">over ::</span> <span class="dt">Lens'</span> s a <span class="ot">-></span> (a <span class="ot">-></span> a) <span class="ot">-></span> s <span class="ot">-></span> s</span>
|
||||
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb35"><pre class="sourceCode haskell code-with-copy"><code class="sourceCode haskell"><span id="cb35-1"><a href="#cb35-1" aria-hidden="true" tabindex="-1"></a><span class="ot">over ::</span> <span class="dt">Lens'</span> s a <span class="ot">-></span> (a <span class="ot">-></span> a) <span class="ot">-></span> s <span class="ot">-></span> s</span>
|
||||
<span id="cb35-2"><a href="#cb35-2" aria-hidden="true" tabindex="-1"></a>over ln f <span class="ot">=</span> runIdentity <span class="op">.</span> ln (<span class="dt">Identity</span> <span class="op">.</span> f)</span>
|
||||
<span id="cb35-3"><a href="#cb35-3" aria-hidden="true" tabindex="-1"></a></span>
|
||||
<span id="cb35-4"><a href="#cb35-4" aria-hidden="true" tabindex="-1"></a><span class="ot">over ::</span> <span class="dt">Traversal'</span> s a <span class="ot">-></span> (a <span class="ot">-></span> a) <span class="ot">-></span> s <span class="ot">-></span> s</span>
|
||||
<span id="cb35-5"><a href="#cb35-5" aria-hidden="true" tabindex="-1"></a>over ln f <span class="ot">=</span> runIdentity <span class="op">.</span> ln (<span class="dt">Identity</span> <span class="op">.</span> f)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||||
<span id="cb35-5"><a href="#cb35-5" aria-hidden="true" tabindex="-1"></a>over ln f <span class="ot">=</span> runIdentity <span class="op">.</span> ln (<span class="dt">Identity</span> <span class="op">.</span> f)</span></code></pre></div><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></div>
|
||||
<p>Der Code ist derselbe - nur der Typ ist generischer. Auch die anderen Dinge<br>
|
||||
funktioniert diese Erweiterung (für Identity und Const muss man noch ein paar<br>
|
||||
dummy-Instanzen schreiben um sie von Functor auf Applicative oder Monad zu heben</p>
|
||||
@ -1097,22 +1102,22 @@ steuern. Beispisweise eine Function anwenden auf</p>
|
||||
<h2 class="anchored" data-anchor-id="wie-es-in-lens-wirklich-aussieht">Wie es in Lens wirklich aussieht</h2>
|
||||
<p>In diesem Artikel wurde nur auf Monomorphic Lenses eingegangen. In der richtigen<br>
|
||||
Library ist eine Lens</p>
|
||||
<div class="sourceCode" id="cb36"><pre class="sourceCode haskell code-with-copy"><code class="sourceCode haskell"><span id="cb36-1"><a href="#cb36-1" aria-hidden="true" tabindex="-1"></a><span class="kw">type</span> <span class="dt">Lens'</span> s a <span class="ot">=</span> <span class="dt">Lens</span> s s a a</span>
|
||||
<span id="cb36-2"><a href="#cb36-2" aria-hidden="true" tabindex="-1"></a><span class="kw">type</span> <span class="dt">Lens</span> s t a b <span class="ot">=</span> <span class="kw">forall</span> f<span class="op">.</span> <span class="dt">Functor</span> f <span class="ot">=></span> (a <span class="ot">-></span> f b) <span class="ot">-></span> (s <span class="ot">-></span> f t)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||||
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb36"><pre class="sourceCode haskell code-with-copy"><code class="sourceCode haskell"><span id="cb36-1"><a href="#cb36-1" aria-hidden="true" tabindex="-1"></a><span class="kw">type</span> <span class="dt">Lens'</span> s a <span class="ot">=</span> <span class="dt">Lens</span> s s a a</span>
|
||||
<span id="cb36-2"><a href="#cb36-2" aria-hidden="true" tabindex="-1"></a><span class="kw">type</span> <span class="dt">Lens</span> s t a b <span class="ot">=</span> <span class="kw">forall</span> f<span class="op">.</span> <span class="dt">Functor</span> f <span class="ot">=></span> (a <span class="ot">-></span> f b) <span class="ot">-></span> (s <span class="ot">-></span> f t)</span></code></pre></div><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></div>
|
||||
<p>sodass sich auch die Typen ändern können um z.B. automatisch einen Konvertierten<br>
|
||||
(sicheren) Typen aus einer unsicheren Datenstruktur zu geben.</p>
|
||||
<p>Die modify-Funktion over ist auch</p>
|
||||
<div class="sourceCode" id="cb37"><pre class="sourceCode haskell code-with-copy"><code class="sourceCode haskell"><span id="cb37-1"><a href="#cb37-1" aria-hidden="true" tabindex="-1"></a><span class="op">></span><span class="ot"> over ::</span> <span class="dt">Profunctor</span> p <span class="ot">=></span> <span class="dt">Setting</span> p s t a b <span class="ot">-></span> p a b <span class="ot">-></span> s <span class="ot">-></span> t</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||||
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb37"><pre class="sourceCode haskell code-with-copy"><code class="sourceCode haskell"><span id="cb37-1"><a href="#cb37-1" aria-hidden="true" tabindex="-1"></a><span class="op">></span><span class="ot"> over ::</span> <span class="dt">Profunctor</span> p <span class="ot">=></span> <span class="dt">Setting</span> p s t a b <span class="ot">-></span> p a b <span class="ot">-></span> s <span class="ot">-></span> t</span></code></pre></div><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></div>
|
||||
<blockquote class="blockquote">
|
||||
<p><em>Edward is deeply in thrall to abstractionitis</em> - Simon Peyton Jones</p>
|
||||
</blockquote>
|
||||
<p>Lens alleine definiert 39 newtypes, 34 data-types und 194 Typsynonyme…<br>
|
||||
Ausschnitt</p>
|
||||
<div class="sourceCode" id="cb38"><pre class="sourceCode haskell code-with-copy"><code class="sourceCode haskell"><span id="cb38-1"><a href="#cb38-1" aria-hidden="true" tabindex="-1"></a><span class="co">-- traverseOf :: Functor f => Iso s t a b -> (a -> f b) -> s -> f t</span></span>
|
||||
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb38"><pre class="sourceCode haskell code-with-copy"><code class="sourceCode haskell"><span id="cb38-1"><a href="#cb38-1" aria-hidden="true" tabindex="-1"></a><span class="co">-- traverseOf :: Functor f => Iso s t a b -> (a -> f b) -> s -> f t</span></span>
|
||||
<span id="cb38-2"><a href="#cb38-2" aria-hidden="true" tabindex="-1"></a><span class="co">-- traverseOf :: Functor f => Lens s t a b -> (a -> f b) -> s -> f t</span></span>
|
||||
<span id="cb38-3"><a href="#cb38-3" aria-hidden="true" tabindex="-1"></a><span class="co">-- traverseOf :: Applicative f => Traversal s t a b -> (a -> f b) -> s -> f t</span></span>
|
||||
<span id="cb38-4"><a href="#cb38-4" aria-hidden="true" tabindex="-1"></a></span>
|
||||
<span id="cb38-5"><a href="#cb38-5" aria-hidden="true" tabindex="-1"></a><span class="ot">traverseOf ::</span> <span class="dt">Over</span> p f s t a b <span class="ot">-></span> p a (f b) <span class="ot">-></span> s <span class="ot">-></span> f t</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
||||
<span id="cb38-5"><a href="#cb38-5" aria-hidden="true" tabindex="-1"></a><span class="ot">traverseOf ::</span> <span class="dt">Over</span> p f s t a b <span class="ot">-></span> p a (f b) <span class="ot">-></span> s <span class="ot">-></span> f t</span></code></pre></div><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></div>
|
||||
<p>dafuq?</p>
|
||||
|
||||
|
||||
@ -1133,7 +1138,7 @@ Ausschnitt</p>
|
||||
a.appendChild(i);
|
||||
window.document.body.appendChild(a);
|
||||
}
|
||||
window.setColorSchemeToggle(window.hasAlternateSentinel())
|
||||
setColorSchemeToggle(hasAlternateSentinel())
|
||||
const icon = "";
|
||||
const anchorJS = new window.AnchorJS();
|
||||
anchorJS.options = {
|
||||
|
Reference in New Issue
Block a user