如何自动化函数式编程,并避免重复。

huangapple go评论65阅读模式
英文:

How to automate functional programing, and avoid repeating myself

问题

在我的gulpfile.js中,我有以下两个函数,它们都使用gulp-nun-jucks来渲染两个不同的视图...


function genNunJucks(cb) {
    return src(paths.views.src)
        .pipe(nunjucksRender({
            path: ['src/views/'], // 字符串或数组
            ext: '.html',
            inheritExtension: false,
            envOptions: {
                watch: true
            },
            manageEnv: manageEnvironment,
            loaders: null
        }))
        .pipe(htmlbeautify({
            indentSize: 2,
            "eol": "\n",
            "indent_level": 0,
            "preserve_newlines": false
        }))
        .pipe(dest(paths.views.dest))
    cb();
}

function genNunJucks2(cb) {
    return src(paths.views.src2)
        .pipe(nunjucksRender({
            path: ['src/views/'], // 字符串或数组
            ext: '.html',
            inheritExtension: false,
            envOptions: {
                watch: true
            },
            manageEnv: manageEnvironment,
            loaders: null
        }))
        .pipe(htmlbeautify({
            indentSize: 2,
            "eol": "\n",
            "indent_level": 0,
            "preserve_newlines": false
        }))
        .pipe(dest(paths.views.dest2))
    cb();
}

在处理gulp进程的返回区域时,如何将这两个函数合并为一个执行相同任务的函数呢?你会注意到我的 path.views.src(x) 是我在重复这个过程的原因。

也许有一种循环处理的方法,可以查看完整的路径数组 paths.views.src: ['','',''] 吗?

非常感谢任何提示。

英文:

Within my gulpfile.js I have the following two functions that are making use of rendering gulp-nun-jucks for 2 different views...


function genNunJucks(cb) {
    return src(paths.views.src)
        .pipe(nunjucksRender({
            path: ['src/views/'], // String or Array
            ext: '.html',
            inheritExtension: false,
            envOptions: {
                watch: true
            },
            manageEnv: manageEnvironment,
            loaders: null
        }))
        .pipe(htmlbeautify({
            indentSize: 2,
            "eol": "\n",
            "indent_level": 0,
            "preserve_newlines": false
        }))
        .pipe(dest(paths.views.dest))
    cb();
}

function genNunJucks2(cb) {
    return src(paths.views.src2)
        .pipe(nunjucksRender({
            path: ['src/views/'], // String or Array
            ext: '.html',
            inheritExtension: false,
            envOptions: {
                watch: true
            },
            manageEnv: manageEnvironment,
            loaders: null
        }))
        .pipe(htmlbeautify({
            indentSize: 2,
            "eol": "\n",
            "indent_level": 0,
            "preserve_newlines": false
        }))
        .pipe(dest(paths.views.dest2))
    cb();
}

When it comes to working within the return area of gulp processes, how can I combine these two functions to be one function that does the same job? You will notice that my path.views.src(x) is the reason I am doing duplicating the process.

Perhaps there is there some kind of lopping process that I can apply that looks through a full array of paths.views.src:['','','']?

Many thanks on any tips

答案1

得分: 1

You can make use of closures and a form of currying for this, and create a function that gets src as a parameter and returns the genNunJucks that you want to use.

function createGenNumChunksFunction(src) {
  return function genNunJucks(cb) {
    return src(src)
      .pipe(nunjucksRender({
        path: ['src/views/'], // String or Array
        ext: '.html',
        inheritExtension: false,
        envOptions: {
          watch: true
        },
        manageEnv: manageEnvironment,
        loaders: null
      }))
      .pipe(htmlbeautify({
        indentSize: 2,
        "eol": "\n",
        "indent_level": 0,
        "preserve_newlines": false
      }))
      .pipe(dest(paths.views.dest))
    cb();
  }
}

And instead of passing genNunJucks at the place where you use it right now, you can pass createGenNumChunksFunction(paths.views.src) and createGenNumChunksFunction(paths.views.src2).

Here is a simplified version that can be executed in the snippet and illustrates how it works:

function createGenNumChunksFunction(src) {
  return function genNunJucks() {
    console.log(src)
  }
}

function callPassedFunction(cb) {
  console.log('function will be called')
  cb();
}

callPassedFunction(createGenNumChunksFunction('src1'))
callPassedFunction(createGenNumChunksFunction('src2'))

An additional note - which is probably not relevant for your case - if you want to use the genNunJucks directly without the need to "create" it first, you could keep it outside of the createGenNumChunksFunction.

function genNunJucks(src) {
  console.log(src)
}

function createGenNumChunksFunction(src) {
  return function() {
    return genNunJucks(src)
  }
}

function callPassedFunction(cb) {
  console.log('function will be called')
  cb();
}

callPassedFunction(createGenNumChunksFunction('src1'))
callPassedFunction(createGenNumChunksFunction('src2'))
genNunJucks('src3')
英文:

You can make use of closures and a form of currying for this, and create a function that gets src as parameter and returns the genNunJucks that you want to use:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

function createGenNumChunksFunction(src) {
return function genNunJucks(cb) {
return src(src)
.pipe(nunjucksRender({
path: [&#39;src/views/&#39;], // String or Array
ext: &#39;.html&#39;,
inheritExtension: false,
envOptions: {
watch: true
},
manageEnv: manageEnvironment,
loaders: null
}))
.pipe(htmlbeautify({
indentSize: 2,
&quot;eol&quot;: &quot;\n&quot;,
&quot;indent_level&quot;: 0,
&quot;preserve_newlines&quot;: false
}))
.pipe(dest(paths.views.dest))
cb();
}
}

<!-- end snippet -->

And instead of passing genNunJucks at the place where you use it right now you can pass createGenNumChunksFunction(paths.views.src) and createGenNumChunksFunction(paths.views.src2)

Here is a simplified version that can be executed in the snipped and illustrates how it works:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

function createGenNumChunksFunction(src) {
return function genNunJucks() {
console.log(src)
}
}
function callPassedFunction(cb) {
console.log(&#39;function will be called&#39;)
cb();
}
callPassedFunction(createGenNumChunksFunction(&#39;src1&#39;))
callPassedFunction(createGenNumChunksFunction(&#39;src2&#39;))

<!-- end snippet -->

An additional note - which is probably not relevant for your case - if you want to use the genNunJucks directly without the need to "create" it first, you could keep it outside of the createGenNumChunksFunction.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

function genNunJucks(src) {
console.log(src)
}
function createGenNumChunksFunction(src) {
return function() {
return genNunJucks(src)
}
}
function callPassedFunction(cb) {
console.log(&#39;function will be called&#39;)
cb();
}
callPassedFunction(createGenNumChunksFunction(&#39;src1&#39;))
callPassedFunction(createGenNumChunksFunction(&#39;src2&#39;))
genNunJucks(&#39;src3&#39;)

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年8月5日 05:09:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/76839118.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定