用红色语言创建一系列数字

时间:2017-10-10 17:44:43

标签: list rebol red

我有一些简单的问题,因此我将它们放在一起:

  1. 创建一系列数字的最佳方法是什么。下面的工作,但有一些更简单的方法,如1:10可用于某些语言?

    const mongoose = require('mongoose') const schema = mongoose.Schema const promise = require('bluebird') const bcrypt = promise.promisifyAll(require('bcrypt')) function hashPassword(user, option) { const SALT_FACTOR = 8 if (!user.isModified('password')) { return; } return bcrypt .genSaltAsync(SALT_FACTOR) .then(salt => bcrypt.hashAsync(user.password, salt, null)) .then(hash => { user.setDataValue('password', hash) }) } // create schema and model const userSchema = new schema({ email: { type: String, required: true, unique: true }, password: { type: String, required: true } }) userSchema.pre('create', function(next) { hashPassword() }) userSchema.pre('update', function(next) { hashPassword() }) userSchema.pre('save', function(next) { hashPassword() }) const user = mongoose.model('user', userSchema) user.prototype.compairePassword = function (password) { return bcrypt.compareAsync(password, this.password) } module.exports = user

    myseries: []

    repeat i 10 [append myseries i]

  2. (1a。为什么上面的代码没有在这个页面上制作通常的代码块?)

    1. 同样,创建一系列10个元素的最佳方法是什么,所有元素都初始化为0或""?我是否还必须使用print myseriesrepeat i 10以及连续loop 10最初为空白的系列?

    2. 此外,我应该在最初创建系列时指定以下代码中的元素数量吗?不这样做的缺点是什么?

      myseries:制作块! 10

    3. 感谢您的帮助。

2 个答案:

答案 0 :(得分:6)

1)我倾向于选择COLLECT

myseries: collect [repeat i 10 [keep i]]

2)参见ARRAY功能:

>> array/initial 10 0
== [0 0 0 0 0 0 0 0 0 0]

你也可以传递一个匿名函数:

>> i: 0 array/initial 10 does [i: i + 1]
== [1 2 3 4 5 6 7 8 9 10] 

3)一般来说,最好使用myseries: make block! 10(如果块的大小未知,则只为0)以避免不必要的陷阱!见To copy or not to copy, that is the question& Is REBOL a Pure Functional Language?

答案 1 :(得分:0)

1)我目前还不知道更好的方法,尽管在https://gitter.im/red/...上有关于范围数据类型或用于实现的讨论

a)它成了一个块。你可以看到probe myseries。你期待什么?

2)>> append/dup [] 0 10 == [0 0 0 0 0 0 0 0 0 0]

3)如果你没有初始化/保留所需的内存,红色必须猜测需要多少内存。这可能超过需要或更少。如果少于Red则必须分配另一块内存。如果您需要更多内存,这可能会发生几次。如果它想要连续内存,也许它必须在内存块周围移动,但我不知道所使用的策略。