### ग्रैडल एकीकरण
Publié le 12 May 2022
Mind map

फलनात्मक प्रोग्रामिंग
अभ्यास पुस्तक से लिए गए हैं:
# ग्रेडल एकीकरण JBake को JBake Gradle प्लगइन का उपयोग करके या JBake CLI को सीधे बुलाकर Gradle बिल्ड में एकीकृत किया जा सकता है:
----
tasks.register<JavaExec>("bake") {
mainClass.set("org.jbake.launcher.Main")
classpath = configurations["jbake"]
args = listOf(projectDir.absolutePath, "$buildDir/jbake")
} +
लेखक: Massimo Carli +
रिपॉज़िटरी: https://github.com/kodecocodes/fpk-materials
=== सिंटैक्स
==== पहला प्रोग्राम टेस्ट
अवधारणाएँ: स्क्रीन प्रदर्शन, फ़ंक्शन +
प्रथम कार्यक्रम परीक्षण:https://github.com/cheroliv/cheroliv.com/blob/master/codes/src/test/kotlin/programming/FirstProgramTest.kt[source](No output)
==== संयोजित फ़ंक्शन परीक्षण
धारणाएँ: स्मृति, चर, मान, वस्तु, फ़ंक्शन विस्तार +
(No output)https://github.com/cheroliv/cheroliv.com/blob/master/codes/src/test/kotlin/programming/ConcatFunctionTest.kt[source]+
==== जन्मदिन संदेश परीक्षण आउटपुट
अवधारणा: समुच्चय, लूप +
https://developer.android.com/codelabs/basic-android-kotlin-training-first-kotlin-program?continue=https%3A%2F%2Fdeveloper.android.com%2Fcourses%2Fpathways%2Fandroid-basics-kotlin-one%23codelab-https%3A%2F%2Fdeveloper.android.com%2Fcodelabs%2Fbasic-android-kotlin-training-first-kotlin-program[### Gradle एकीकरण
JBake को Gradle बिल्ड्स में JBake Gradle प्लगिन का उपयोग करके या JBake CLI को सीधे कॉल करके एकीकृत किया जा सकता है:
----
tasks.register<JavaExec>("bake") { mainClass.set("org.jbake.launcher.Main") classpath = configurations["jbake"] args = listOf(projectDir.absolutePath, "$buildDir/jbake") } ```
Kotlin का परिचय] +
जन्मदिन संदेश टेस्ट आउटपुट:https://github.com/cheroliv/cheroliv.com/blob/master/codes/src/test/kotlin/programming/BirthdayMessageTestOutput.kt[source]Veuillez fournir le texte français à traduire.
=== Kotlin का कोर्स
video::YRjY3jRrQYY[youtube]
=== जावा में संग्रहों पर ट्यूटोरियल
https://docs.oracle.com/javase/tutorial/collections/index.html[संग्रह,window=_blank]
कलेक्शन इंटरफ़ेस का कोर +
image:../../img/0036_training_kotlin_post/colls-coreInterfaces.gif[Collections] +
=== बुनियादी कार्यात्मक
==== घोषणात्मक X आदेशात्मक दृष्टिकोण
[source,kotlin]
----
package functional
import kotlin.test.Test
import kotlin.test.assertEquals
class DeclarativeTests {
val input = listOf(
"123", "abc", "1ds", "987", "abdf", "1d3", "de1", "88", "101"
)
fun imperativeSum(list: List<String>): Int {
var sum = 0
for (item in list) {
try {
sum += item.toInt()
} catch (_: NumberFormatException) {
}
}
return sum
}
@Test
fun `test imperative approach`() {
imperativeSum(input).run {
println("Sum $this")
assertEquals(1299, this)
}
}
fun isValidNumber(s: String) = try {
s.toInt()
true
} catch (_: NumberFormatException) {
false
}
fun declarativeSum(list: List<String>) = list
.filter(::isValidNumber)
.map(String::toInt)
.sum()
@Test
fun `test declarative approach`() {
assertEquals(1299, declarativeSum(input).apply {
println("Sum $this")
})
}
}
----
==== अभ्यास 1.1
sumInRange फ़ंक्शन लागू करें, जो मान जोड़ता है में +
एक List<String> एक दिए गए अंतराल में। हस्ताक्षर है:
[source,kotlin]
----
fun sumInRange(input: List<String>, range: IntRange): Int
----
[source,kotlin]
----
@Test
fun `Exercise 1_1`() {
assertEquals(4, sumInRange(
listOf("1", "10", "a", "7", "ad2", "3"),
1..5
).apply { println("sumInRange 1..5: $this") }
)
}
----
इसे आज़माएँ और अपने उत्तर की जाँच करें के साथhttps://github.com/cheroliv/cheroliv.com/blob/master/codes/src/test/kotlin/functional/DeclarativeTests.kt[समाधान, windows="_blank].
==== उच्च-क्रम फ़ंक्शन
[source,kotlin]
----
package functional
import java.io.ByteArrayOutputStream
import java.io.PrintStream
import java.lang.System.*
import java.lang.Thread.sleep
import kotlin.math.sign
import kotlin.test.Test
import kotlin.test.assertEquals
class BasicsHOFTests {
val ONE_SECOND = 1000L
@Test
fun `high order function`() {
//capture de la sortie standard
val standardOut: PrintStream? = out
val outputStreamCaptor = ByteArrayOutputStream()
setOut(PrintStream(outputStreamCaptor))
3.times { println("Hello") }
assertEquals(
buildString {
repeat(3) { append("Hello\n") }
deleteAt(length - 1)
}, outputStreamCaptor
.toString()
.trim()
)
//libération de la sortie standard
setOut(standardOut)
}
fun Int.times1(fn: () -> Unit) {
for (i in 1..this) {
fn()
}
}
fun Int.times2(fn: () -> Unit) {
for (i in 1..this) fn()
}
fun Int.times3(fn: () -> Unit) =
(1..this).forEach { fn() }
fun Int.times4(fn: () -> Unit) =
repeat((1..this).count()) { fn() }
fun Int.times(fn: () -> Unit) =
(1..this).forEach { _ -> fn() }
}
----
==== अभ्यास 1.2
क्रोनो लागू करें, जो एक प्रकार के फ़ंक्शन को स्वीकार करता है`() ->` +
इकाई इनपुट के रूप में ली जाती है और निष्पादन में लगने वाला समय लौटाती है। हस्ताक्षर : +
[source,kotlin]
----
fun chrono(fn : () -> Unité) : Long
----
[source,kotlin]
----
@Test
fun `Exercise 1_2`() {
val waitOneSec = { sleep(ONE_SECOND) }
chrono(waitOneSec).apply {
println("chrono: $this")
assertEquals(1, sign)
}
}
----
इसे आज़माएँ और अपने उत्तर की जाँच करें के साथhttps://github.com/cheroliv/cheroliv.com/blob/master/codes/src/test/kotlin/functional/BasicsHOFTests.kt[हल, windows="_blank].
==== रचना
[source,kotlin]
----
package functional
import kotlin.test.Test
import kotlin.test.assertEquals
fun double(x: Int): Int = 2 * x
fun square(x: Int): Int = x * x
fun squareAndDouble1(x: Int) = double(square(x))
infix fun <A, B, C> ((A) -> B).compose(g: (B) -> C)
: (A) -> C = { a -> g(this(a)) }
class CompositionTests {
@Test
fun composition_impure() {
assertEquals(200, double(square(10)))
assertEquals(200, squareAndDouble1(10))
}
@Test
fun composition_pure() {
val squareAndDouble = ::square compose ::double
assertEquals(200, squareAndDouble(10))
}
}
----
==== # एक नया JBake प्रोजेक्ट प्रारम्भ करें
jbake -i
# साइट को बेक (जनरेट) करें jbake -b
# स्थानिक रूप से बेक करके सर्व करें jbake -b -s
# बेक करें और परिवर्तनों की निगरानी करें jbake -b --reset
# स्रोत और गंतव्य निर्दिष्ट करें jbake source_folder output_folder
# बेक करने से पहले आउटपुट निर्देशिका साफ़ करें jbake -b . output --reset ``` शुद्ध फ़ंक्शन और परीक्षणीयता
[source,kotlin]
----
package functional
import java.io.ByteArrayOutputStream
import java.io.PrintStream
import java.lang.System.out
import java.lang.System.setOut
import kotlin.test.Test
import kotlin.test.assertEquals
var count = 0
//impure car une variable global subit un effet de bord
fun impure(value: Int): Int {
count++
return value + count
}
//impure car utilisation de la sortie standard qui fait muter le system
fun addOneAndLog(x: Int): Int {
val result = x + 1
println("New Value is $result")
return result
}
//pure
fun addOne(x: Int) = (x + 1).run {
Pair(this, "New Value is $this")
}
class PureTests {
@Test
fun `impure fonction`() {
assertEquals(3, impure(2))
val standardOut = out
val outputStreamCaptor = ByteArrayOutputStream()
setOut(PrintStream(outputStreamCaptor))
addOneAndLog(3)
assertEquals(
"New Value is 4",
outputStreamCaptor
.toString()
.trim()
)
setOut(standardOut)
}
@Test
fun `pure fonction`() {
addOne(3).run {
assertEquals(4, first)
assertEquals("New Value is 4", second)
}
}
}
----
==== अपवाद प्रबंधन
[source,kotlin]
----
package functional
import org.junit.jupiter.api.assertThrows
import kotlin.Result.Companion.failure
import kotlin.Result.Companion.success
import kotlin.test.Test
import kotlin.test.assertEquals
//NumberFormatException est un effet de bord qui rend la fonction impure
fun strToInt(str: String) = str.toInt()
//pure
fun strToIntOrNull(str: String) = try {
str.toInt()
} catch (nfe: NumberFormatException) {
null
}
//pure avec gestion de l'exception plus élégante
fun strToIntResult(str: String): Result<Int> =
try {
success(str.toInt())
} catch (nfe: NumberFormatException) {
failure(nfe)
}
class ExceptionHandlingTests {
@Test
fun impure() {
assertThrows<NumberFormatException> { strToInt("foo") }
assertEquals(1, strToInt("1"))
}
@Test
fun pure() {
assertEquals(null, strToIntOrNull("foo"))
assertEquals(1, strToIntOrNull("1"))
}
@Test
fun `pure avec result`() {
assertEquals(1, strToIntResult("1").getOrNull())
assertEquals(
"For input string: \"foo\"",
strToIntResult("foo")
.exceptionOrNull()
?.message
)
}
}
----
=== मुख्य बिंदु
* जबकि वस्तु‑उन्मुख प्रोग्रामिंग का अर्थ है वस्तुओं के साथ प्रोग्रामिंग करना।
फ़ंक्शनल प्रोग्रामिंग का मतलब फ़ंक्शन के साथ प्रोग्राम करना है। आप एक समस्या को कई उप‑समस्याओं में तोड़ते हैं, जिन्हें आप मॉडल करते हैं के साथ फ़ंक्शन।
* उच्च कोटि के फ़ंक्शन अन्य फ़ंक्शन को इनपुट के रूप में स्वीकार करते हैं या अन्य फ़ंक्शन लौटाते हैं
वे वापसी मान के रूप में कार्य करते हैं। श्रेणी सिद्धांत संयोजन का सिद्धांत है, और आप इसे समझने के लिए उपयोग करते हैं कैसे अपने फ़ंक्शन को संयोजित करें एक कार्यक्रम में एक शुद्ध फ़ंक्शन का आउटपुट मान केवल इसके इनपुट पैरामीटर पर निर्भर करता है, और यह इसका कोई दुष्प्रभाव नहीं।
* एक साइड इफ़ेक्ट वह कार्य है जो कोई फ़ंक्शन बाहरी दुनिया पर करता है। यह
यह मानक आउटपुट में एक लॉग या किसी वैश्विक चर के मान में संशोधन हो सकता है। फ़ंक्शनल प्रोग्रामिंग शुद्ध कार्यों के लिए काम करती है, लेकिन यह भी प्रदान करती है इन अशुद्ध फ़ंक्शन को शुद्ध फ़ंक्शन में बदलने के लिए उपकरण। आप एक अशुद्ध फ़ंक्शन को शुद्ध बना सकते हैं प्रभावों को स्थानांतरित करके उन्हें परिणाम मान का भाग
* फ़ंक्शनल प्रोग्रामिंग संयोजन का विषय है।
त्रुटि प्रबंधन साइड इफ़ेक्ट का एक विशिष्ट मामला है, और कोटलिन आपको उपकरण देता है उन्हें कार्यात्मक रूप से प्रबंधित करने के लिए।
=== जावा8 को गहराई से समझें: लैम्ब्डा अभिव्यक्तियाँ और फंक्शनल इंटरफ़ेस
video::20waNRw6wMA[youtube,list=PLzzeuFUy_Cng0wZhqbnkvWAW0d2fdVfyQ]
=== श्रेणी सिद्धांत
गणितीय श्रेणियों का सिद्धांत:
video::LVHoROSF3KA[youtube]
=== फ़ंक्शन के मूल सिद्धांत
==== अभ्यास 2.1
क्या आप एक फ़ंक्शन का उदाहरण लिख सकते हैं जो अलग-अलग मानों को मैप करता है? +
जिसके डोमेन में सीमा में असमान मान होते हैं, जैसे f(b) और f(c) नीचे दिए गए चित्र में? +
इसे आज़माएँ, फिर चुनौती प्रोजेक्ट को देखें कि आपने कैसे किया, एक समाधान के लिए। +
आप लिंक का पालन करके सलाह और एक स्पष्टीकरण पाएँगे की ओर https://github.com/cheroliv/cheroliv.com/blob/master/codes/src/test/kotlin/functional/BasicsHOFTests.kt[समाधान, खिड़कियाँ="_blank]. +
==== अभ्यास 2.2
Can you write the inverse function of twice ? +
विलोम फलन के लिए डोमेन और श्रेणी क्या हैं? +
Check out the challenge project and Appendix B for the solution.
[source,kotlin]
----
fun chrono(fn : () -> Unité) : Long
----
[source,kotlin]
----
@Test
fun `Exercise 1_2`() {
val waitOneSec = { sleep(ONE_SECOND) }
chrono(waitOneSec).apply {
println("chrono: $this")
assertEquals(1, sign)
}
}
----
इसे आज़माएँ और अपने उत्तर के साथ जाँचेंhttps://github.com/cheroliv/cheroliv.com/blob/master/codes/src/test/kotlin/functional/BasicsHOFTests.kt[हल, windows="_blank].