Skip to content
This repository has been archived by the owner on Jun 30, 2020. It is now read-only.

Generic sample function #2

Open
nevrome opened this issue Oct 29, 2019 · 11 comments
Open

Generic sample function #2

nevrome opened this issue Oct 29, 2019 · 11 comments
Labels
enhancement New feature or request

Comments

@nevrome
Copy link
Owner

nevrome commented Oct 29, 2019

I would like to implement a generic sample function for arrays in V. The inspiration for this is base::sample() in R.

Related stackoverflow question: https://stackoverflow.com/questions/57997114/how-to-write-a-generic-sample-function-in-v

@nevrome nevrome added the enhancement New feature or request label Oct 29, 2019
@Delta456
Copy link

Delta456 commented Oct 29, 2019

Please check the reply.

@Delta456
Copy link

Templates still don't work because of module error.

@nevrome
Copy link
Owner Author

nevrome commented Oct 30, 2019

Thank you for helping me in this question @Delta456
Could you elaborate a little bit what is broken? Modules and Templates just don't work together atm?

The following independent code works:

import rand

fn main() {

  arr := [0].repeat(10)
  println(sample(arr, 5).str())


  arr2 := ["test"].repeat(10)
  println(sample(arr2, 5).str())

}

fn sample<T>(arr[] T, size int) []T {

  mut res := [arr[1]].repeat(size)
  for i := 0; i < size; i++ {
    res[i] = arr[rand.next(arr.len)]
  }

  return res

}

I can't really test the module version any more because I have an another issue with module building. I will open another issue for that.

@nevrome
Copy link
Owner Author

nevrome commented Oct 30, 2019

If you adjust your answer accordingly ([0].repeat(size) has to be changed to [arr[1]].repeat(size) or a more fancy solution to define the array type dynamically), then I will accept it and mark my question as solved.

@Delta456
Copy link

Delta456 commented Oct 30, 2019

Thank you for helping me in this question @Delta456
Could you elaborate a little bit what is broken? Modules and Templates just don't work together atm?

The following independent code works:

import rand

fn main() {

  arr := [0].repeat(10)
  println(sample(arr, 5).str())


  arr2 := ["test"].repeat(10)
  println(sample(arr2, 5).str())

}

fn sample<T>(arr[] T, size int) []T {

  mut res := [arr[1]].repeat(size)
  for i := 0; i < size; i++ {
    res[i] = arr[rand.next(arr.len)]
  }

  return res

}

I can't really test the module version any more because I have an another issue with module building. I will open another issue for that.

There are some problems with public generics function and method. It is similar to vlang/v#2547 and vlang/v#2528

@nevrome
Copy link
Owner Author

nevrome commented Oct 30, 2019

There are some problems with public generics function and method. It is similar to vlang/v#2547 and vlang/v#2528

Ah - I see. Generics currently seem to be a hot topic anyway. I guess it's best to just wait until this is sorted out.

@Delta456
Copy link

As generics are back in the new backend. Your function works. Here my implementation of sample in V which is better.

@nevrome
Copy link
Owner Author

nevrome commented Jun 30, 2020

Excellent! Thanks for keeping me in the loop. I guess your module random is a better implementation of what I had in mind for this package, so I will deprecate it. Didn't touch this in many months.

@Delta456
Copy link

@nevrome You can improve and keep this. Someone will use it.

@nevrome
Copy link
Owner Author

nevrome commented Jun 30, 2020

Maybe I will come back to it eventually, but right now I have enough side projects. If you find some code in here that you find useful, then feel free to steal it. 😄

@Delta456
Copy link

@nevrome You should archive it for now. Don't forget to make more modules with V! Its way better now!!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants